1
我有一個具有文件夾列表嵌套的字典,像這樣的地方2013_09_10
是根文件夾:查找一個Python字典的關鍵,返回其父母
{'2013_09_10': {'master.tex': None, 'master.log': None, 'master.pdf': None, 'Makefile': None, 'pieces': {'rastin.tex': None, '02 Human Information Processing.txt': None, 'p1.tex': None, 'p3.tex': None, '03 Models and Metaphors.txt': None, 'p2.tex': None}, 'master.aux': None, 'front_matter.tex': None}}
什麼我試圖完成的是給定一個文件(例如p1.tex)的名稱,打印文件的實際路徑
2013_09_10/pieces/p1.tex
我創建了這段代碼,但只給了我的所有文件的名稱,而不是它的父母鍵(directiory):
def get_files(directory):
for filename in directory.keys():
if not isinstance(directory[filename], dict):
print filename
else:
get_files(directory[filename])
輸出:
master.tex
master.log
master.pdf
Makefile
rastin.tex
02 Human Information Processing.txt
p1.tex
p3.tex
03 Models and Metaphors.txt
p2.tex
master.aux
front_matter.tex
我認爲我的代碼只需要一個簡單的修改來完成我想要的,但我不能排序了這一點。
您的循環不變的是,你將打印的文件名。嘗試改變它,使它變成打印目錄,然後打印剩下的部分。 – Boud