Romano
Romano

Reputation: 67

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

I have this python code, to add a torrent file:

try:
    with open(full_path_to_torrent, 'rb') as file:
        c.add_torrent(file)
    logger_internet.info(
        'The .torrent transfered to Transmition %s', full_path_to_torrent)
except FileNotFoundError as e:
    print(f"Error: Torrent file '{full_path_to_torrent}' not found.")
except Exception as e:
    print(f"Error: An unexpected error occurred: {e}")

below i have this code to move the .torrent file:

DESTINATION_FOLDER = 'D:/Torrent_files'
def move_torrent_file(path):
    try:
        shutil.move(full_path_to_torrent, DESTINATION_FOLDER)
        logger_internet.info("Successfully moved .torrent file at %s", path)
    except FileNotFoundError:
        logger_internet.error("File %s does not exist.", path)
    except Exception as e:
        logger_internet.error(
            "An error occurred while moving the .torrent file: %s", e)
move_torrent_file(full_path_to_torrent)

But it gave the error: Exception has occurred: PermissionError [WinError 32] The process cannot access the file because it is being used by another process

i have read: "In Python, when you open a file using the with open(...) statement, the file is automatically closed after the block of code within the with statement finishes executing. This is because the with statement sets up a context and ensures that the file is properly closed once the block is exited, regardless of whether an exception occurs."

Also the file is move, but a first file remains in the initial folder. what i am missing? i assume the first block of code is the problem.

Upvotes: 0

Views: 255

Answers (0)

Related Questions