【发布时间】:2020-08-20 23:22:14
【问题描述】:
我正在尝试借助“shutil”模块将文件“A”的内容传输到“临时”文件中。但是,我收到以下错误:
[WinError 32] The process cannot access the file because it is being used by another process
我也尝试在 google 中研究同样的错误,但是没有一个对我有帮助。我不确定出了什么问题。
我使用的是 windows 10(64 位),我的 python 版本是 3.7。
具体编码如下:
import csv
import shutil
from tempfile import NamedTemporaryFile
import os
class csvtest():
def editcsv1(self,filename):
filename="data.csv"
tempfile=NamedTemporaryFile(delete=False,dir=r"C:\Users\Sahil\Desktop\python")
with open(filename,"r") as csvfile2,open(tempfile.name,"w") as temp_file:
reader=csv.reader(csvfile2)
writer=csv.writer(temp_file)
for row in reader:
writer.writerow(row)
csvfile2.close()
temp_file.close()
os.unlink(temp_file.name)
shutil.move(temp_file.name,filename)
abc=csvtest()
abc.editcsv1(filename)
'''
根据要求,回溯消息如下: '''
runfile('C:/Users/Sahil/Desktop/python/stackoverflow5may.py',wdir='C:/Users/Sahil/Desktop/python')
文件“”,第 1 行,在 runfile('C:/Users/Sahil/Desktop/python/stackoverflow5may.py', wdir='C:/Users/Sahil/Desktop/python')
运行文件中的文件“C:\Users\Sahil\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 827 行 execfile(文件名,命名空间)
文件“C:\Users\Sahil\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 110 行,在 execfile exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/Sahil/Desktop/python/stackoverflow5may.py”,第 23 行,在 abc.editcsv1(文件名)
文件“C:/Users/Sahil/Desktop/python/stackoverflow5may.py”,第 20 行,在 editcsv1 中 shutil.move(temp_file.name,filename)
文件“C:\Users\Sahil\Anaconda3\lib\shutil.py”,第 578 行,移动中 os.unlink(src)
PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用:'C:\Users\Sahil\Desktop\python\tmp2gibk4eh'
'''
【问题讨论】:
-
可以添加python的错误回溯信息吗? Is 向我们展示了失败的行。
-
在运行脚本之前是否关闭了每个程序中的文件?
-
@luigigi - 该文件在此程序中打开两次
-
@tdelaney 谢谢您的回复。不幸的是,回溯消息太长,不允许我在这里输入。但是,如果这有帮助,请告诉我。抱歉,我无法对其进行格式化:''' 文件“C:\Users\Sahil\Anaconda3\lib\shutil.py”,第 578 行,在 move os.unlink(src) PermissionError: [WinError 32] The process cannot access该文件,因为它正在被另一个进程使用:'C:\\Users\\Sahil\\Desktop\\python\\tmp2gibk4eh''''
-
请在您的问题中发布完整的追溯。不在 cmets 中。你可以编辑你的问题
标签: python