简体   繁体   English

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

[英]renaming files in python causes Device or resource busy

I am writing a script with python. 我正在用python编写脚本。

First, let me show you the exception: 首先,让我告诉您例外情况:

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

Then here are method causing the problem: 然后是导致问题的方法:

def make_backup(self, file_path):
    name = os.path.basename(file_path)
    folder_path = os.path.dirname(file_path)
    if not name.startswith('backup_'):
        new_name = os.path.join(folder_path,"backup_"+name)
        os.rename(file_path, new_name)

def make_backups(self, folder_path):
    for file in folder_path:
        file_path = os.path.join(folder_path, file)
        self.make_backup(file_path)    

Do I need to create threads to solve this problem? 我是否需要创建线程来解决此问题? I guess the cause is os is been used by multiple progress and this cause the exception. 我想原因是操作系统被多个进程使用,这导致了异常。

Thanks for any help! 谢谢你的帮助!

You definitely don't need threads to solve this. 你绝对不需要的线程来解决这个问题。 It just looks you're renaming something that is actually in use... 看起来您正在重命名实际上正在使用的内容...

Looking at the Linux manpage for the rename() call , you might get EBUSY (error 16) when renaming a directory that's in use: 重命名正在使用的目录时,请在Linux联机帮助页上查看named()调用 ,您可能会得到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. 重命名失败是因为oldpath或newpath是某个进程正在使用的目录(可能是当前工作目录或根目录,或者因为它已打开供读取)或系统正在使用(例如,作为挂载点) ),而系统认为这是一个错误。 (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.) (请注意,在这种情况下不需要返回EBUSY -无论如何进行重命名都没有错-但如果系统无法处理这种情况,则允许返回EBUSY。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM