0

I am trying to change the contents of a destination file with the help of some source file. When the work is done, I am making sure that I have closed all the files in code (ofcourse, I have made sure that it is not opened in any editor too.)

    def write_to_file(self, _source_path, _destination_path):
        f_source = open(_source_path, 'r')
        f_destination = open(_destination_path, 'r')
        f_temp = open(self.temp_path, 'w+')

        while source_line or destination_line:...

        f_temp.close()
        f_destination.close()
        f_source.close()
        shutil.move(self.temp_path, _destination_path)
        return

I am getting below error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/rohit.tayal/PycharmProjects/AutoNVMerge/MainActivity.py", line 151, in <module>
    script.write_to_file(source_path, destination_path)
  File "C:/Users/rohit.tayal/PycharmProjects/AutoNVMerge/MainActivity.py", line 62, in write_to_file
    shutil.move(self.temp_path, _destination_path)
  File "C:\Anaconda\envs\AutoNVMerge\lib\shutil.py", line 581, in move
    os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\auto_nv_merge.xml'

C:\auto_nv_merge.xml is my temp file.

6
  • 2
    See the error message. Another program is currently using that file - you can use Task Manager to see which program that is. Probably Excel, so if you close the file in Excel then this might work. Commented Dec 23, 2019 at 6:49
  • If you are sure you closed the file in all editors and the computer tells you that it is still open, you should believe the computer. It does not lie. You are not running HAL 9000. Commented Dec 23, 2019 at 6:59
  • 1
    restart your computer and run that first Commented Dec 23, 2019 at 7:12
  • @zvone, funny one.. :D Commented Dec 23, 2019 at 7:45
  • @ksooklall, thanks, I should have done this before only.. It worked. Maybe it was open somewhere after all. I thought I am not closing it properly. Commented Dec 23, 2019 at 7:45

1 Answer 1

1

When working with

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\auto_nv_merge.xml'

you have 2 options, be a hero and try to hunt down the process that is holding that file hostage or just restart your computer.

I usually go with the latter, restart

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.