我有一个具有以下结构的数据库:
dataset1
- subject-1
- session-1
- group1
- subject-1_session-1_other-stuff-01.json
- subject-1_session-1_other-stuff-01.txt
- subject-1_session-1_other-stuff-02.json
- subject-1_session-1_other-stuff-02.txt
- group2
- files-i-dont-care-about.bvec
- group3
- subject-1_session-1_other-different-stuff-01.json
- subject-1_session-1_other-different-stuff-01.txt
- subject-1_session-1_other-different-stuff-02.json
- subject-1_session-1_other-different-stuff-02.txt
- session2
- group1
- subject-1_session-2_other-stuff-01.json
- subject-1_session-2_other-stuff-01.txt
- subject-1_session-2_other-stuff-02.json
- subject-1_session-2_other-stuff-02.txt
- group2
- differently-named-files-i-dont-care-about.bvec
- group3
- subject-1_session-2_other-different-stuff-01.bval
- subject-1_session-2_other-different-stuff-01.bvec
- subject-1_session-2_other-different-stuff-02.bval
- subject-1_session-2_other-different-stuff-02.bvac
- subject-2
- ^ etc
我想删除文件的开头(subject-x_session-x_),并保留other-stuff *。*和other-different-stuff *。*等。我在这里使用*作为UNIX扩展,但不确定是否可以在变量内部将其用作输入。我认为一个问题是,当脚本尝试将新文件名放置在正确的目录中时,该目录不会动态定向到文件的来源。希望我仍然可以使用os.walk(),但是似乎该函数一次检索了一个数组,并且在我可以调用os.rename()之前结束了。我认为我需要能够在os.walk()完成之前分段调用os.rename()。否则,我必须在每个特定目录上运行脚本来更改名称,但这是一个大型数据库。这就是我的开始:
root = /full-path/database/
find = "subject-1"
# ^ ideally this would find any string starting with subject-{any number}_session-{any number} but I'd be happy just to get the explicit string to work
replace_with = ""
list = os.walk(root)
for root, dirs, files in list:
for file in files:
new_filename = file.replace(find, replace_with)
src = os.path.join(root, file)
dst = os.path.join(root, new_filename)
os.rename(src, dst)
print(filename + " renamed to " + new_filename)
当os时脚本失败。重命名()被调用。遍历深度不同的目录并一次性更改这些文件是否合理?
我正在考虑类似map函数或较大函数内部的多个for循环之类的东西,但是我碰到了墙。