So I made this script to sort a folder into subfolders of different types and it worked! But now I would like it to sort folders within the folder I tell it to sort. I tried recursion but it didn't work? is my syntax wrong? Also how would you get it to move file types of x up a directory into the appropriately sorted folder in the folder I told the script to sort? If that makes sense.
Here's my code:
#!/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"
shutil.copytree(src,dst)
shutil.rmtree(src)
should get you where you want ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.