当前位置: 首页 > 知识库问答 >
问题:

如何解决这个python错误-PermissionError:[WinError 32]进程无法访问文件,因为它正被另一个进程使用:

鲍理
2023-03-14

我正试图在“shutil”模块的帮助下将文件“A”的内容传输到“temp”文件中。但是,我得到了以下错误:

[WinError 32] The process cannot access the file because it is being used by another process

我也试着在谷歌上搜索同样的错误,但是没有一个能帮到我。我不确定出了什么问题。

我使用的是Windows10(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)

'''

根据请求,html" target="_blank">回溯消息如下所示:“”

运行文件('C:/用户/萨希尔/桌面/python/stackoverflow5may.py',wdir='C:/用户/萨希尔/桌面/python')

文件“”,第1行,在runfile('C:/Users/Sahil/Desktop/python/stackoverflow5may.py',wdir='C:/Users/Sahil/Desktop/python'中)

runfile execfile(文件名、命名空间)中的文件“C:\Users\Sahil\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第827行

文件“C:\Users\Sahil\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第110行,在execfile exec中(compile(f.read(),filename,'exec'),命名空间)

文件“C:/Users/Sahil/Desktop/python/stackoverflow5may.py”,第23行,在abc中。editcsv1(文件名)

在editcsv1shutil.move(temp_file.name,文件名)中的文件"C:/用户/萨希尔/桌面/Python/stackoverflow5may.py",第20行

文件“C:\Users\Sahil\Anaconda3\lib\shutil.py”,第578行,在move os中。取消链接(src)

PermissionError:[WinError 32]进程无法访问文件,因为它正被另一个进程使用:'C:\用户\Sahil\Desktop\python\tmp2gibk4eh'

'''

共有1个答案

姜淇
2023-03-14

NamedTemporyFile返回一个打开的文件对象,但您尝试使用open(tempfile.name,“w”)作为临时文件再次打开它。您的for循环中有一个bug(关闭每写入一行的文件)。所以

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)

 类似资料:
  • 问题内容: 我的代码用于一个脚本,该脚本查看一个文件夹并删除分辨率为1920x1080的图像。我的问题是我的代码运行时; 我收到此错误消息: 只需确认一下,Python是我计算机上运行的唯一程序。是什么导致此问题,我该如何解决? 问题答案: 您的过程就是打开文件的过程(仍然存在)。您需要先关闭它,然后再删除它。 我不知道PIL是否支持上下文,但是是否支持: 进入之前,请确保删除(并关闭文件)。 如

  • 我的脚本搜索特定目录中的所有pdf文件,然后从pdf中提取一个id,并在文件中组织pdf。例如我有: 我想这样组织它们: 下面的脚本做的工作,但我认为只有最后一个文件输出以下错误: 回溯(最近一次调用):文件“C:\Users\user\Downloads\aa\project.py”,第74行,在操作系统中。rename(source,dest)PermissionError:[WinError

  • 我是C#新手,连接Firebird数据库时遇到问题。我想让我的程序访问Firebird数据库[FDB格式文件]。我有问题,请参见下面的代码: 这段代码允许我读取FDB文件并提取数据。当代码第一次执行时,没有错误或问题,但是当我再次执行时,这个错误会显示出来: 进程无法访问文件“C:\Users\ACC-0001”。FDB’因为它正被另一个进程使用。

  • 下面是使用Ucanaccess Jdbc驱动程序从Microsoft Access文件filename.accdb获取连接的代码。但在运行此代码时,它会抛出异常,就像已经使用的文件一样。 但是我想在其他应用程序使用MSAccess数据库文件时并发地使用它。 当我运行上面的代码时,出现了如下异常: net.ucanaccess.jdbc.ucanaccesssqlexception:UCAEXC::

  • 我正在尝试测试我自己的antiweb版本,可以在这里找到。但是,我正在使用Pythons单元测试模块对其进行测试。代码如下: 除了功能外,所有功能都正常工作。在执行unittest时,如果不拆下,temp文件夹及其内容将被完美创建。但是使用功能,我得到一个错误: 当我再看临时文件夹时,文件夹本身仍然在那里,但现在是空的。这将是太多,包括我的反网络文件在这里,所以我有它在这里再次链接,如果你需要它。