在python中重命名文件会导致设备或资源繁忙

2024-09-27 21:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python编写脚本。在

首先,让我向您展示例外情况:

Traceback (most recent call last):

File "/home/jiewei/PycharmProjects/TestEngine/test/test_multiple_classes.py", line 23, in test_create_symlink_and_run_test_in_devbench_but_is_called_in_vdi
    self.sm.make_backups(self.dev_path)
File "testengine/SymlinkManger.py", line 24, in make_backups
    self.make_backup(file_path)
File "testengine/SymlinkManger.py", line 19, in make_backup
    os.rename(file_path, new_name)
OSError: [Errno 16] Device or resource busy

下面是导致问题的方法:

^{pr2}$

我需要创建线程来解决这个问题吗?我想原因是操作系统被多个进程使用,这导致了异常。在

谢谢你的帮助!在


Tags: pathinpytestself脚本makeline
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:54

你绝对不需要线程来解决这个问题。它只是看起来你在重命名一些实际上正在使用的东西。。。在

查看Linux manpage for the rename() call,在重命名正在使用的目录时,可能会得到EBUSY(错误16):

The rename fails because oldpath or newpath is a directory that is in use by some process (perhaps as current working directory, or as root directory, or because it was open for reading) or is in use by the system (for example as mount point), while the system considers this an error. (Note that there is no requirement to return EBUSY in such cases—there is nothing wrong with doing the rename anyway—but it is allowed to return EBUSY if the system cannot otherwise handle such situations.)

相关问题 更多 >

    热门问题