1
votes

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'

'''

1
Can you add the error traceback message from python? Is shows us the line that fails.tdelaney
did you close the file in every program before you are running the script?luigigi
@luigigi - the file is opened twice in this programtdelaney
@tdelaney Thankyou for your response. Unfortunately, the trace back message is too long and not allowing me to input here . However, let me know if this helps. Apologies i am unable to format it : ''' 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' '''solguy
and please post the full traceback in your question. not in the comments. you can edit your questionluigigi

1 Answers

3
votes

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)