I have this python code, to add a torrent file:
我有这个python代码,添加一个torrent文件:
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:
下面是用来移动.torrent文件的代码:
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
但它给出了错误:发生异常:PermissionError[WinError 32]该进程无法访问该文件,因为它正被另一个进程使用
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."
我读到过:“在Python中,当您使用WITH OPEN(...)语句打开文件时,在WITH语句中的代码块执行完毕后,该文件将自动关闭。这是因为WITH语句设置了一个上下文,并确保一旦退出该块,文件将被正确关闭,而不管是否发生异常。”
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.
此外,文件将被移动,但第一个文件保留在初始文件夹中。我错过了什么?我想第一段代码就是问题所在。
更多回答
Missing to try on file.close()
to close it manually at the end of "with ... :" block?
缺少尝试文件的方法。是否在“with...:”块的末尾手动关闭文件。
Did not work @HarshitGupta
@HarshitGupta不起作用
我是一名优秀的程序员,十分优秀!