我已經嘗試了劇本的多次反覆,但現在又回到一個空白的記事本+Python的 - 將文件移動到包含文件名
盯着的一部分文件夾我有形式6K文件夾:
Reading Festival 1999
Reading Festival 2000
Reading Festival 2001
Leeds Festival 1999
Leeds Festival 2000
Leeds Festival 2001
Download Festival 2005
Download Festival 2006
...
在同一個文件夾我的文件有一個長長的清單形式
Artist at {Festival Name} by Photographer - UID
哪裏Artist
是誰的人在節日出場,Photographer
是人WH Ø把它和UID
是一個唯一的ID
我的問題是我怎麼會去通過文件夾循環,對於每一個,看文件名中包含文件夾的名稱,如果這樣做,將文件移動那裏。
import os
rootdir = 'C:\Users\Alan\Desktop\VF'
for subdir, dirs, files in os.walk(rootdir):
for d in dirs:
for file in files:
the_file = os.path.join(subdir, file)
if d in the_file:
new_loc = subdir + '\\' + d + '\\' + file
os.rename(the_file, new_loc)
我有這樣的代碼,我相信應該的工作,但我擔心的是,它會通過已經夾內的所有圖像讀取。我將如何避免這種情況?
我是否正確,所有的文件和文件夾都在同一級別?我想知道是否需要使用os.walk() - 它遍歷樹。你需要嗎? –
不 - 我意識到這一點,所以看着listdir ....感謝推動 – pee2pee