【问题标题】:Shutil.copytree ----> WindowsError: [Error 3] The system cannot find the path specified:Shutil.copytree ----> WindowsError: [错误3] 系统找不到指定的路径:
【发布时间】:2020-06-24 03:52:38
【问题描述】:

我正在尝试从源中提取内容并移至目标文件夹。

folder1 = 2018
folder2 = 8    
folder3 = 3
    source = os.path.join("C:\\","Pizza","Sammy","Logs", "Archive", "DataLog_Private" ,str(folder1),"0" + str(folder2),"0" + str(folder3))

    destination = os.path.join("C:\\","Users", "alex", "Desktop", "logPull" , "DataLog_Private" ,str(folder1),"0" + str(folder2),"0" + str(folder3))

shutil.copytree(source,destination)

我也试过这条路。

#source = r"C://Pizza//Sammy//Logs//Archive//DataLog_Private//%s//%s//%s//" %(str(folder1),"0" + str(folder2),"0" + str(folder3))
#destination = r"C://Users//alex//Desktop//logPull//DataLog_Private//%s//%s//%s//" %(str(folder1),"0" + str(folder2),"0" + str(folder3))

在使用 copytree 时,两条路径都出现此错误

WindowsError: [Error 3] The system cannot find the path specified'C:\\Pizza\\Sammy\\Logs\\Archive\\DataLog_Private\\2018\\08\\03/*.*'

请帮忙。

【问题讨论】:

    标签: python windows operating-system shutil


    【解决方案1】:

    以下在 Python 3.6 中适用于我,注意环境变量的使用。

    import os
    import shutil
    
    folder1 = 2018
    folder2 = 8    
    folder3 = 3
    
    drive = os.path.join(os.getenv("HOMEDRIVE"), os.sep)
    date_path = os.path.join(f"{folder1}", f"{folder2:02}", f"{folder3:02}")
    
    source = os.path.join(
        drive, "Pizza","Sammy", "Logs", "Archive", "DataLog_Private", date_path
    )
    destination = os.path.join(
        os.getenv("USERPROFILE"), "Desktop", "logPull", "DataLog_Private", date_path
    )
    
    shutil.copytree(source, destination)
    

    HOMEDRIVE 应该指向安装 Windows 的任何磁盘。列出默认环境变量here

    f"{expression}" 表示法称为 f 字符串。这是在 Python 3.6 here's the PEP 中引入的。在括号内添加:02 会使数字前导0。

    【讨论】:

    • 感谢您的回复!是 os.getenv("HOMEDRIVE") == " C:/" 吗?另外, date_path = os.path.join(f"{folder1}", f"{folder2:02}", f"{folder3:02}") 中的 'f' 是什么?代表?
    • 编辑了我的答案来解释这两个。
    猜你喜欢
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 2018-03-26
    • 2020-05-27
    相关资源
    最近更新 更多