错误 32,Python,另一个进程正在使用文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17095309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error 32, Python, file being used by another process
提问by Max Kim
I have a simple program, which looks for all compressed folders in a directory, targets one compressed file, gets an excel file located inside the compressed file and moves it to another location (it does this for every excel file, for how many ever compressed folders):
我有一个简单的程序,它查找目录中的所有压缩文件夹,定位一个压缩文件,获取位于压缩文件内的 excel 文件并将其移动到另一个位置(它对每个 excel 文件执行此操作,对于压缩过的文件数量)文件夹):
path = 'C:\Users\me\Documents\Extract'
new_path = 'C:\Users\me\Documents\Test'
i = 0
for folder in os.listdir(path):
path_to_folder = os.path.join(path, folder)
zfile = zipfile.ZipFile(os.path.join(path, folder))
for name in zfile.namelist():
if name.endswith('.xls'):
new_name = str(i)+'_'+name
new_path = os.path.join(new_path, new_name)
zfile.close()
#os.rename(path_to_folde, new_path) -- ERROR HERE
shutil.move(path_to_folde, new_path) -- AND ERROR HERE
i += 1
I have tried 2 ways to move the excel file os.renameand shutil.move. I keep on getting an error:
我尝试了 2 种方法来移动 excel 文件os.rename和shutil.move. 我不断收到错误:
WindowsError: [Error 32] The process cannot access the file beacause it is being used by another process.
WindowsError: [错误 32] 进程无法访问该文件,因为它正被另一个进程使用。
I don't understand why this error persists, since I have closed every folder.
我不明白为什么这个错误仍然存在,因为我已经关闭了每个文件夹。
采纳答案by OregonTrail
path = 'C:\Users\me\Documents\Extract'
destination_path = 'C:\Users\me\Documents\Test'
i = 0
for folder in os.listdir(path):
path_to_zip_file = os.path.join(path, folder)
zfile = zipfile.ZipFile(path_to_zip_file)
for name in zfile.namelist():
if name.endswith('.xls'):
new_name = str(i)+'_'+name
new_path = os.path.join(destination_path, new_name)
# This is obviously going to fail because we just opened it
shutil.move(path_to_zip_file, new_path)
i += 1
zfile.close()
Changed some of the variable names in your code snippet. Do you see your problem now? You're trying to move the zip file that yourprocess has open. You'll need to copy the .xlsfile to your destination using the zipfile module.
更改了代码片段中的一些变量名称。你现在看到你的问题了吗?您正在尝试移动您的进程已打开的 zip 文件。您需要.xls使用 zipfile 模块将文件复制到目的地。
回答by Mike El Hymanson
If you are on a windows computer go to the task manager and hit the processes tab. Scroll down to anything that says python and end the process. You may have had python running with something else. Then try running your python program again and it should work.
如果您使用的是 Windows 计算机,请转到任务管理器并点击进程选项卡。向下滚动到任何说 python 的内容并结束该过程。您可能让 python 与其他东西一起运行。然后再次尝试运行您的 python 程序,它应该可以工作。

