Python shutil.copy2-不同文件系统和伪文件的行为

Python shutil.copy2-不同文件系统和伪文件的行为,python,shutil,Python,Shutil,我已经编写了一个小脚本来同步两个目录。该脚本允许同步某些文件类型,并允许通过校验和或日期进行文件比较。 比较后,应复制的文件列表将提供给使用shutil.copy2()模块的文件复制过程。 在各种测试运行中,我发现shutil.copy2有一种奇怪的行为,我没有解决办法: 第一: 如果源是ext3卷,目标是fat32(记忆棒),则shutil将引发错误。 我认为这是因为shutil.copy2也尝试复制元数据,而fat32不支持这种做法。但我不知道如何用try:except:语句捕捉这个错误 第

我已经编写了一个小脚本来同步两个目录。该脚本允许同步某些文件类型,并允许通过校验和或日期进行文件比较。 比较后,应复制的文件列表将提供给使用shutil.copy2()模块的文件复制过程。 在各种测试运行中,我发现shutil.copy2有一种奇怪的行为,我没有解决办法:

第一: 如果源是ext3卷,目标是fat32(记忆棒),则shutil将引发错误。 我认为这是因为shutil.copy2也尝试复制元数据,而fat32不支持这种做法。但我不知道如何用try:except:语句捕捉这个错误

第二: 第二个问题更难。源和目标都是ext3卷。在源代码中有一些完整Linux目录树的备份。当我的工具尝试同步这些目录树时,脚本将以无休止的循环运行,直到我的系统分区耗尽空间。 我尝试修复此行为,并在开始复制过程之前使用stat模块检查源文件是否为常规文件,但没有帮助。具有奇怪行为的文件是/proc/661/fd/3。可能还有一些,但我无法测试,因为我的系统在试图复制此文件时由于内存消耗而冻结

我已经为这两个问题找了好几天的解决方案,我希望她身边有经验的程序员能支持我

谢谢你的帮助

下面是我的filecopy过程的代码:

def _filecopy(file_list, from_dir, to_dir, overwrt):
print "Files and directories will be processed: "
for file_tupel in file_list:
    source_file_name = from_dir + file_tupel[1] + file_tupel[0]
    try:
        filemode = os.stat(source_file_name).st_mode
    except:
        filemode = 33205 
    if S_ISREG(filemode):
        target_dir_name = to_dir + file_tupel[1]
        if not os.path.isdir(target_dir_name):
            print "Create directory " + target_dir_name
            _mkdir(target_dir_name)
        target_file_name = target_dir_name + file_tupel[0]
        if os.path.isfile(target_file_name) and overwrt == "mark":
            name_appendix = "_marked_by_sync_as_different_on_" + time.strftime("%a_%d_%b_%Y_%H-%M-%S", time.localtime())
            if target_file_name.find(".") == -1:
                new_target_file_name = target_file_name + name_appendix
                new_source_file_name = source_file_name + name_appendix
            else:
                new_target_file_prefix = target_file_name.rpartition(".")[0]
                new_target_file_extension = target_file_name.rpartition(".")[2]
                new_target_file_name = new_target_file_prefix + name_appendix + "." + new_target_file_extension
                new_source_file_prefix = source_file_name.rpartition(".")[0]
                new_source_file_name = new_source_file_prefix + name_appendix + "." + new_target_file_extension
            print "Rename file " + target_file_name + " in " + new_target_file_name
            os.rename(target_file_name, new_target_file_name)
            print "Copy file " + new_target_file_name + " to " + new_source_file_name
            try:
                shutil.copy2(new_target_file_name, new_source_file_name)
            except:
                print "Could not copy " + new_target_file_name + " to " + new_source_file_name
        print "Copy file " + source_file_name + " to " + target_file_name
        try:
            shutil.copy2(source_file_name, target_file_name)
        except:
            print "Could not copy " + source_file_name + " to " + target_file_name
    else:
        print source_file_name + " seems to be no regular file and will not be copied."
在遵循第1号答案的提示后,copy2语句变为:

    shutil.copyfile(source_file_name, target_file_name)
    try:
        #try to set permission bits
        shutil.copymode(new_target_file_name, new_source_file_name)
    except:
        print "Permission bits could not be copied"
    try:
        #try to copy metadata
        shutil.copystat(new_target_file_name, new_source_file_name)
    except:
        print "Metadata could not be copied"  
shutil.copy2()
只是
shutil.copy()
后跟
shutil.copystat()
。要解决第一个问题,您可能需要复制,并将
copystat()
部分包含在try-except块中。您应该能够从错误回溯中找出要捕获的异常

您可能根本不想复制
/proc
文件系统。无论何时调用脚本,我都会排除这个问题。

shutil.copy2()
只是
shutil.copy()
后跟
shutil.copystat()
。要解决第一个问题,您可能需要复制,并将
copystat()
部分包含在try-except块中。您应该能够从错误回溯中找出要捕获的异常


您可能根本不想复制
/proc
文件系统。无论何时调用脚本,我都会排除此错误。

请发布两个错误的完整回溯。对于/proc错误,没有任何消息。系统刚刚冻结。文件系统错误的回溯是:file“/home/piet/Desktop/server\u disk\u backup/music kopieren/pietsync.py”,第260行,在文件复制shutil.copy2(新的目标文件名,新的源文件名)UnboundLocalError:AssignMen之前引用的局部变量“new_target_file_name”请发布这两个错误的完整回溯。对于/proc错误,没有任何消息。系统刚刚冻结。文件系统错误的回溯是:file“/home/piet/Desktop/server\u disk\u backup/music kopieren/pietsync.py”,第260行,在文件复制shutil.copy2(新的目标文件名,新的源文件名)UnboundLocalError:在assignmenMy/proc问题之前引用了局部变量“new\u target\u file\u name”:我试图使我的同步工具尽可能通用。所以我不想按名称排除某些文件或目录。也许这个问题还有另一个解决方案。嗨,谢谢你的快速回复。如果我使用shutil.copyfile()而不是shutil.copy()或shutil.copy2(),那么在不同的文件系统之间进行复制的解决方案是有效的。但是,也许有更好的方法可以做到这一点,而不会对每个已同步的文件都产生错误。My/proc问题:我试图让我的同步工具尽可能普遍可用。所以我不想按名称排除某些文件或目录。也许这个问题还有另一个解决方案。嗨,谢谢你的快速回复。如果我使用shutil.copyfile()而不是shutil.copy()或shutil.copy2(),那么在不同的文件系统之间进行复制的解决方案是有效的。但是,也许有更好的方法可以做到这一点,而不会对每个同步的文件产生错误。