0

我正在尝试摆脱源文件夹中的非字母数字字符,并将任何具有非字母数字字符的文件重命名为版本,而不使用此代码。但是,每次我运行该模块时,我都会收到此错误,

Traceback (most recent call last):
  File "C:\tempstore\Filescan1", line 18, in <module>
    os.rename(filename,newfilename)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process

请帮忙?

os.chdir("C:\\tempstore\\source")
file_list = glob.glob("*.mpg*")
for filename in file_list:
    if re.search('[^A-Za-z0-9._ ]+',filename,re.U) is not None:
        print filename + " <--Dodgy File"
        print re.sub('[^0-9a-zA-Z+_. ]+','-',filename)+ " <--Fixed File"
        newfilename =  re.sub('[^0-9a-zA-Z+_. ]+','-',filename)
        os.rename(filename,newfilename)
    elif re.search('[^A-Za-z0-9._ ]+',filename,re.U) is None:
        print filename +" <-- Normal File"
        unchanged_list = re.sub('[^A-Za-z0-9._ ]+','_',filename)
        print unchanged_list
4

2 回答 2

0

关闭您的防病毒软件或至少将“访问时”扫描设置为关闭。我还建议使用 re.sub 生成newfilename,然后将其用于打印和重命名,即:

    print re.sub('[^0-9a-zA-Z+_. ]+','-',filename)+ " <--Fixed File"
    newfilename =  re.sub('[^0-9a-zA-Z+_. ]+','-',filename)

应该读:

    newfilename =  re.sub('[^0-9a-zA-Z+_. ]+','-',filename)
    print 'Renaming to:', newfilename

并摆脱示例的最后两行。

于 2013-07-18T11:59:57.250 回答
0

Solved! The reason the error came up was that windows will not let you edit files in an open directory and my program was opening the directory to edit the files. Catch-22 really, you can't edit files in an open directory but you have to open the directory to edit the files. I have moved the files using shutil.copy and then renaming them and this works fine Apologies for my noob behaviour!

于 2013-07-19T08:51:25.173 回答