Error 32, Python, file used by another process

I have a simple program that searches for all compressed folders in a directory, targets one compressed file, gets the excel file located inside the compressed file, and moves it to another location (it does this for every excel file, for which many ever compressed folders):

path = 'C:\Users\me\Documents\Extract' new_path = 'C:\Users\me\Documents\Test' i = 0 for folder in os.listdir(path): path_to_folder = os.path.join(path, folder) zfile = zipfile.ZipFile(os.path.join(path, folder)) for name in zfile.namelist(): if name.endswith('.xls'): new_name = str(i)+'_'+name new_path = os.path.join(new_path, new_name) zfile.close() #os.rename(path_to_folde, new_path) -- ERROR HERE shutil.move(path_to_folde, new_path) -- AND ERROR HERE i += 1 

I tried 2 ways to move excel os.rename and shutil.move . I keep getting the error:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process.

I do not understand why this error persists since I closed every folder.

+8
python
source share
2 answers
 path = 'C:\Users\me\Documents\Extract' destination_path = 'C:\Users\me\Documents\Test' i = 0 for folder in os.listdir(path): path_to_zip_file = os.path.join(path, folder) zfile = zipfile.ZipFile(path_to_zip_file) for name in zfile.namelist(): if name.endswith('.xls'): new_name = str(i)+'_'+name new_path = os.path.join(destination_path, new_name) # This is obviously going to fail because we just opened it shutil.move(path_to_zip_file, new_path) i += 1 zfile.close() 

Changed some variable names in the code snippet. Do you see your problem now? You are trying to move the zip file that was opened by your process. You will need to copy the .xls file to the destination using the zipfile module.

+7
source share

If you are on a Windows computer, go to the task manager and go to the process tab. Scroll down to everything python says and end the process. Maybe you had python working with something else. Then try to run the python program again and it should work.

+2
source share

All Articles