为什么以下python脚本未按预期复制文件?

问题描述

开始时,有人试图将整个硬盘复制到另一个位置。复制某些文件时出错,这些文件的新路径将超过最大值。路径长度。我将所有受影响的文件放在一个文件夹中,我的任务是现在将它们复制到它们所属的位置。

这是我的方法:

  1. 创建字典路径。
  2. 在此字典中为每个要复制的文件设置一个键作为其源路径
  3. 读取包含目标路径的Excel文件,并将其作为值分配给相应的键
  4. 通过在文件名和文件扩展名之间添加“ _mig”来操纵文件名,因此我可以在目的地中看到此文件是我本人复制的,而不是原来复制的。

这或多或少是在“ setOldPath()”和“ setNewPath()”函数中发生的情况。 下列主要功能将所有内容整合在一起,该功能使用paths-dictionary复制文件:

def main():
setOldPath(Path(input("Please paste the source path in here:\n")))
setNewPath(r"\\fe24999\C_CCH_GLW$\40_IT\Projekt_Digitales_Archiv_CS\DLZA-Bosch\Charlie 2020\Masseningest Dais-Ablage\Auswertung MIG-Ordner\2020-08-26_Auswertung-MIG-Ordner.xlsx")
countSuccess = 0
countAll = 0
Errorlog = open("allErrors.log","w",encoding="utf-8")
Correctlog = open("allCopied.log",encoding="utf-8")

for current,new in paths.items():
    try:
        base,extension = os.path.splitext(new)
        test = os.path.exists(new)
        print(test)
        if not os.path.exists(new):     
            shutil.copy(current,winapi_path(new))
            print("first time")
        else:
            print("more times")
            i = 1
            while os.path.exists(os.path.join(new,'{}{}{}'.format(base,i,extension))):
                i += 1
            new= os.path.join(new,extension))
            print(new)
            shutil.copy(current,winapi_path(new))
    except Exception as e:
        print("\nError with %s" % (current))
        Errorlog.write("Error: %s \n could not copy %s \nto \n%s \n\n" % (e,current,new))
        countAll += 1
    else:
        print("\nSuccessfully COPIED")
        Correctlog.write("Successfully copied %s \nto \n%s \n\n" % (current,new))
        countSuccess += 1
        countAll += 1
Errorlog.write("%d out of %d Elements successfully processed." % (countSuccess,countAll))
Errorlog.close()
Correctlog.write("%d out of %d Elements successfully processed." % (countSuccess,countAll))
Correctlog.close()
print("\n%d out of %d Elements successfully processed." % (countSuccess,countAll))

遗憾的是,有很多文件名完全相同,即使它们不同也是如此。所以我不能覆盖现有文件。我在stackoverflow上找到了一个很好的解决方案,并尝试通过在文件名末尾添加递增数字来解决此问题。 总而言之,它似乎对我的大多数文件都运行良好,但是有些文件无法正常工作,我不能说为什么。查看以下内容:

  • 2010年6月23日,ZVEI-Vorstandssitzung的Einladung和Tagesordnung zur-2010März24.msg的Protokoll der Vorstandssitzung
  • 2010年6月23日上午在ZVEI-Vorstandssitzung举行的Einladung und Tagesordnung zur-2010März24站的Protokoll der Vorstandssitzung消息
  • 2010年6月23日,ZVEI-Vorstandssitzung的Einladung和Tagesordnung zur-2010März24.msg的Protokoll der Vorstandssitzung

应将它们全部复制到以下路径:\ FE0VM7775 \ Masseningest_P \ Input \ Portalraum-Ablage-Dais \ G2 Sekretariat \Verbände_Vereine_Unis\ ZVEI \ ZVEI_2010 \ ZVEI_Vorstandssitzung_10032324。

所有路径均已正确写入并分配给我的字典。复制后,目标中只有一个文件-这意味着他在我循环的每一步都将其覆盖,但是为什么?

PS:这就是winapi_path()中发生的情况,它解决了我的问题,即由于路径太长而导致的复制错误。如果Windows对复制的文件有问题对我来说并不重要,那么复制它是唯一重要的事情:

def winapi_path(dos_path,encoding=None):
if (not isinstance(dos_path,str) and 
    encoding is not None):
    dos_path = dos_path.decode(encoding)
path = os.path.abspath(dos_path)
if path.startswith(u"\\\\"):
    return u"\\\\?\\UNC\\" + path[2:]
return u"\\\\?\\" + path

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)