【问题标题】:Files are copied but size remains 0 KB using shutil.copy2使用 shutil.copy2 复制文件但大小仍为 0 KB
【发布时间】:2015-08-24 05:34:57
【问题描述】:

我可以从不同的目录复制文件,但文件内容没有被复制。我不确定我在做什么错误:

import os,zipfile,shutil
rootdir = r'Y:\StorageReports\Mitrend_Reports\test'
adddir=r'Y:\StorageReports\Mitrend_Reports\test\additional'
sheetdir=r'Y:\StorageReports\Mitrend_Reports\test\spreadsheets'
extn="pptx"
extn1="xlsx"

for dirpath, dirnames, files in os.walk(rootdir):
    for i in files:
        if "Validate the Value" or "VNX Additional Details" in i:
        if i.endswith (extn):    
                f=os.path.join(dirpath,i)
                print (f)
                shutil.copy2(f,adddir)
for dirpath, dirnames, files in os.walk(rootdir):
    for j in files:
        #if "Validate the Value" or "VNX Additional Details" in j:
        if j.endswith (extn1):    
            f1=os.path.join(dirpath,j)
            print (f1)
            shutil.copy2(f1,sheetdir)
for dirpath, dirnames, files in os.walk(rootdir):
    for k in files:
        #print (k)
        if "VNX Profile" in k:
            print(k)
            f2=os.path.join(dirpath,k)
            print(f2)
            shutil.copy2(f2,rootdir)

for dirpath, dirnames, files in os.walk(rootdir):
    for l in files:
        #print (k)
        if "Workload Overview" in l:
            print(l)
            f3=os.path.join(dirpath,l)
            print(f3)
            shutil.copy2(f3,rootdir)

我犯了什么错误?

【问题讨论】:

  • 格式化您的代码,以便我们为您提供帮助
  • 我已经编辑了脚本。谢谢
  • rootdir、sheetdir、adddir中是否存在dirpath的目录?
  • 在使用shutil.copy2 之前,您能否打印或查看f 变量的调试器值?
  • @Alexander - 所有这些目录都存在。当我执行复制命令时,所有文件都被复制并显示它们。但是文件的大小是 0 KB。

标签: python file-copying


【解决方案1】:

这只发生在 shutil.copy()、copy2()、copyfile() 对文件没有“干净”访问权限的情况下。在大多数情况下,这是因为您在尝试复制之前没有关闭文件。没有对第一个文件的读取权限也可能导致这种情况。

我刚刚遇到了这个问题,因为我同时安排了两次相同的节目。互相踩踏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    相关资源
    最近更新 更多