繁体   English   中英

如何创建递归Python脚本对文件和文件夹进行排序?

[英]How Can I Create a Recursive Python Script to Sort Files and Folders?

因此,我制作了此脚本,以将文件夹分类为不同类型的子文件夹,并且可以正常工作! 但是现在我希望它对我告诉它排序的文件夹中的文件夹进行排序。 我尝试了递归,但是没有用? 我的语法错误吗? 另外,如何将x的文件类型上移到我告诉脚本进行排序的文件夹中适当排序的文件夹中? 如果这样的话。

这是我的代码:

#!/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)

应该带你到你想要的地方...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM