PermissionError: [WinError 32] The process cannot access the file because it is being used by another process. This error is shown in shutil.move()

180 views Asked by At

shutil.move() function doesn't seem to work properly in the following code and is showing the attached error.

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

if file.is_file():
    # print(file.suffix.lower()
    if file.suffix.lower() in extension_paths:
        destination_path = destination_dir / extension_paths[file.suffix.lower()]
    else:
        destination_path = destination_dir / extension_paths['noname']
    destination_path = add_date_to_file(destination_path)
    destination_path = rename_file(file, destination_path)
    shutil.move(str(file), str(destination_path))

I have been trying to debug this code a lot lately. I found that if I use the below code, the code works fine except both the 'try' and 'except' blocks run. Is that possible? If so, how?

try:
    if file.suffix.lower() in extension_paths:
        destination_path = destination_dir / extension_paths[file.suffix.lower()]
    else:
        destination_path = destination_dir / extension_paths['noname']
    destination_path = add_date_to_file(destination_path)
    destination_path = rename_file(file, destination_path)
    shutil.move(str(file), str(destination_path))
except (Exception,):
    pass

I would be grateful if somebody comes up with the reason of the error. Thanks!

0

There are 0 answers