1
所以我讓這個腳本將一個文件夾排序到不同類型的子文件夾中,它工作正常!但現在我希望它對文件夾內的文件夾進行排序,我告訴它要排序。我嘗試遞歸,但它沒有工作?我的語法錯了嗎?另外你如何獲得它將文件類型的x上移到文件夾中適當排序的文件夾中我告訴腳本進行排序?如果這是有道理的。如何創建遞歸Python腳本來排序文件和文件夾?
這裏是我的代碼:
#!/bin/python
import os
path = raw_input("Enter your folder you would like sorted: ")
def searchFolders(path):
if os.path.exists(path):
dirList = os.listdir(path)
for filename in dirList:
if ".jpg" in filename:
if not os.path.exists(path + "Photos"):
os.makedirs(path + "Photos")
os.rename(path + filename, path + "Photos/" + filename)
elif ".pptx" in filename:
if not os.path.exists(path + "Powerpoints"):
os.makedirs(path + "Powerpoints")
os.rename(path + filename, path + "Powerpoints/" + filename)
elif ".zip" in filename:
if not os.path.exists(path + "Zip Files"):
os.makedirs(path + "Zip Files")
os.rename(path + filename, path + "Zip Files/" + filename)
elif ".dmg" in filename:
if not os.path.exists(path + "Disk Images"):
os.makedirs(path + "Disk Images")
os.rename(path + filename, path + "Disk Images/" + filename)
elif ".mp3" in filename:
if not os.path.exists(path + "Music"):
os.makedirs(path + "Music")
os.rename(path + filename, path + "Music/" + filename)
elif ".pdf" in filename:
if not os.path.exists(path + "Pdf"):
os.makedirs(path + "Pdf")
os.rename(path + filename, path + "Pdf/" + filename)
elif ".cpp" in filename:
if not os.path.exists(path + "C++"):
os.makedirs(path + "C++")
os.rename(path + filename, path + "C++/" + filename)
elif ".psd" in filename:
if not os.path.exists(path + "Photoshop"):
os.makedirs(path + "Photoshop")
os.rename(path + filename, path + "Photoshop/" + filename)
elif ".dng" in filename:
if not os.path.exists(path + "Photos/Raw Photos"):
os.makedirs(path + "Photos/Raw Photos")
os.rename(path + filename, path + "Photos/Raw Photos/" + filename)
elif not "." in filename:
folderPath = path + filename
searchFolders(folderPath)
else:
if not os.path.exists(path + "Random"):
os.makedirs(path + "Random")
os.rename(path + filename, path + "Random/" + filename)
print "Sorting Complete"
else:
print "Folder Does not exist"
我會叫這個在我的節目結束或開始? – Chriscross
無論你想將src文件夾移動到dst文件夾... –