使用.cmd文件运行python脚本

时间:2013-11-08 07:13:54

标签: python regex cmd

我刚刚创建了这个脚本,现在我正试图让它再次运行。我使用.cmd文件来运行我制作的python脚本。

以下是.cmd文件的内容:

    @setlocal enableextensions & "C:/Program Files/Autodesk/Maya2012/bin/mayapy.exe" -x "%~f0" %* & goto :EOF

    import sys
    import time
    sys.path.append(r"C:\Users\Court\Desktop\Extra Space\Q8\Intro to 3D Scripting")
    import project_outline
    project = raw_input
    project_outline.kickstart(project)

    raw_input("Press enter to close this window.")

我将在一分钟内粘贴python脚本的内容。这样做的目的是创建一个独立的功能,将某些类型的文件组织到特定的文件夹中。我输入源文件的地址然后它应该做它的事情。我似乎遇到的问题是python文件无法执行。

这是python脚本

    #bring in some important tools
    import os
    import sys
    import re
    import shutil


    # function to set up everything needed to sort the files
    def kickstart(project):
        #variable that will be used to create file names
        subGroup = ["mayaFiles", "maxFiles", "tgaImages", "jpgImages", "psdFiles", "udkFiles", "upkFiles"]
        #variable that will be used to identify file type
        fileExtension = [[".ma", ".mb"], [".max"], [".tga"], [".jpg"], [".psd"], [".udk"], [".upk"]]
        #creates a variable that holds the destination path for when folders are created
        path = "C:\Users\Court\Desktop\Extra Space\Q8\Intro to 3D Scripting\organizedFiles"
        #iterator
        i=0
        #iterate through each grouping, initiate subroutine to create fil folders and move files to the right folder
        for sg in subGroup:
            for extension in fileExtension[i]:
                fileList = fileFinder(project, extension)
                subfileFinder(path, project, sg, fileList)  
            i=i+1
    #defines a routine that will step through a designated folder and find files of certain types
    def fileFinder(project, fileExtension):
        fileList = []
        #for loop for stepping through a source folder
        for root, dirs, files in os.walk(project):
            #for loop for analyzing each file in the folder
            for name in files:
                #series of if statements that analyzes the file extension to see what kind of file it is and store it under the relevant variable
                if os.path.splitext(name)[1] == fileExtension:
                    fileList.append(os.path.join(root, name))
        return fileList

    #defines a routine that further analyzes the name of each file 
    def subfileFinder(path, project, subGroup, fileList):
        for name in fileList:
            INeedToMove = True
            for category in ["Char_", "Env_", "T_"]:
            #will try to match the name of the file to a string of characters
            #if the value of match is true
                if re.search(category, name):
                    INeedToMove = False
                        #attempt to create a directory with specific name
                        try:
                            print ("making directory for " + path + "/" + subGroup + "/" + category)
                            os.makedirs(path + "/"  + subGroup + "/" + category)
                        #if the directory can't be made, move on
                        except:
                            pass
                        print ("Moving " + name + " to " + path + "/" + subGroup + "/" + category)
                        location = (project + "/" + name)
                        destination = (path + "/" + subGroup + "/" + category)
                        try:
                            shutil.copy(location, destination)
                        except:
                            print ("Failed to move")
                    if INeedToMove:
                        try:
                            print ("Making directory for " + subGroup)
                            os.makedirs(path + "/" + subGroup)
                        #if the directory can't be made, move on
                        except:
                            pass
                        print ("Moving " + name + " to " + path + "/" + subGroup)
                        try:
                            shutil.copy((project + "/" + name), (path + "/" + subGroup + "/" + name))
                        except:
                            print ("Failed to move.")

我将非常感谢我能得到的所有帮助。我已经尝试了我所知道的一切来解决这个问题。我是一个相对较新的脚本编写,所以我相信这里有人可以告诉我一些我可能没有想到的东西。感谢。

0 个答案:

没有答案