【问题标题】:How to tackle this python error - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:如何解决这个 python 错误 - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
【发布时间】: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


【解决方案1】:

NamedTemporyFile 返回一个打开的文件对象,但您尝试使用open(tempfile.name,"w") as temp_file 第二次打开它。您的 for 循环中有一个错误(关闭每行写入的文件)。所以,

import csv
import shutil
from tempfile import NamedTemporaryFile
import os

class csvtest():

    def editcsv1(self,filename):  
        filename="data.csv"
        with NamedTemporaryFile(dir=r"C:\Users\Sahil\Desktop\python",
                mode="w", delete=False) as tempfile:
            with open(filename,"r") as csvfile2:
                reader=csv.reader(csvfile2)
                writer=csv.writer(tempfile)
                writer.writerows(reader)
        shutil.move(temp_file.name,filename)
        os.remove(f.name)


abc=csvtest()
abc.editcsv1(filename)

【讨论】:

  • 感谢您纠正我的代码。感谢你们,我今天学到了一些新东西。我确实已经弄清楚了出现此错误的原因以及解决方案(感谢您的社区)。但是,现在我遇到了新错误(需要一个类似字节的对象,而不是“str”)。我会尝试自己解决这个问题,如果需要帮助,我会用新的查询回复你们。你们做得很好!上帝保佑你!
  • 我的错,我没有意识到 tempfile 默认是二进制的。此外,虽然删除的效果不如我。
  • 我以为我之前确实回复过....奇怪的评论不知何故被删除了。也许,我想感谢你们帮助我,是的,上面的代码我没有收到任何错误,结果符合预期。你们摇滚!!
猜你喜欢
  • 2022-06-16
  • 1970-01-01
  • 2022-12-27
  • 2022-01-24
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
相关资源
最近更新 更多