Python 使用shutil.copy2复制文件,但大小仍为0 KB
我可以从不同的目录复制文件,但不会复制文件内容。我不确定我犯了什么错误:Python 使用shutil.copy2复制文件,但大小仍为0 KB,python,file-copying,Python,File Copying,我可以从不同的目录复制文件,但不会复制文件内容。我不确定我犯了什么错误: 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"
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)
我犯了什么错误?只有当shutil.copy()、copy2()、copyfile()没有对文件的“干净”访问权限时,才会发生这种情况。在大多数情况下,这是因为您在尝试复制文件之前没有关闭该文件。对第一个文件没有读取权限也可能导致此问题
我只是有这个问题,因为我在同一时间安排了两次相同的节目。互相践踏 只有当shutil.copy()、copy2()、copyfile()没有对文件的“干净”访问权限时,才会发生这种情况。在大多数情况下,这是因为您在尝试复制文件之前没有关闭该文件。对第一个文件没有读取权限也可能导致此问题
我只是有这个问题,因为我在同一时间安排了两次相同的节目。互相践踏 格式化您的代码,以便我们可以帮助您我已经编辑了脚本。谢谢您rootdir、sheetdir和adddir中是否存在dirpath目录?在使用
shutil.copy2
之前,是否可以打印或在调试器中查看f
变量的值?@Alexander-所有这些目录都存在。当我执行copy命令时,所有文件都会被复制并显示出来。但是文件的大小是0 KB。请格式化您的代码,以便我们可以帮助您。我已经编辑了脚本。谢谢您rootdir、sheetdir和adddir中是否存在dirpath目录?在使用shutil.copy2
之前,是否可以打印或在调试器中查看f
变量的值?@Alexander-所有这些目录都存在。当我执行copy命令时,所有文件都会被复制并显示出来。但是文件的大小是0 KB。请格式化您的代码,以便我们可以帮助您。我已经编辑了脚本。谢谢您rootdir、sheetdir和adddir中是否存在dirpath目录?在使用shutil.copy2
之前,是否可以打印或在调试器中查看f
变量的值?@Alexander-所有这些目录都存在。当我执行copy命令时,所有文件都会被复制并显示出来。但是文件的大小是0 KB。