solguy
solguy

Reputation: 23

How to tackle this python error - PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:

I am trying to transfer the content of the file ‘A’ into ‘temp’ file with the help of ‘shutil’ module. But, I am getting below error:

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

I also tried to research in google for the same error, however none of them helped me. I am not sure what is going wrong.

I am using windows 10 (64 bit), my python version is 3.7.

The details of the coding are as follows:

    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)

'''

As requested, the traceback message as below : '''

runfile('C:/Users/Sahil/Desktop/python/stackoverflow5may.py',wdir='C:/Users/Sahil/Desktop/python')

File "", line 1, in runfile('C:/Users/Sahil/Desktop/python/stackoverflow5may.py', wdir='C:/Users/Sahil/Desktop/python')

File "C:\Users\Sahil\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\Users\Sahil\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Sahil/Desktop/python/stackoverflow5may.py", line 23, in abc.editcsv1(filename)

File "C:/Users/Sahil/Desktop/python/stackoverflow5may.py", line 20, in editcsv1 shutil.move(temp_file.name,filename)

File "C:\Users\Sahil\Anaconda3\lib\shutil.py", line 578, in move os.unlink(src)

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\Sahil\Desktop\python\tmp2gibk4eh'

'''

Upvotes: 1

Views: 8178

Answers (1)

tdelaney
tdelaney

Reputation: 77407

NamedTemporyFile returns an open file object but you try to open it a second time with open(tempfile.name,"w") as temp_file. You had a bug in your for loop (closing the files per row written). So,

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)

Upvotes: 5

Related Questions