问题
I am able to copy files from different directories but the file contents are not copied. I am not sure what mistake I am doing:
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)
What is the mistake I am making?
回答1:
This only happens when shutil.copy(), copy2(), copyfile() does NOT have "clean" access to the file. in most cases this is because you have not closed the file before trying to copy it. Not having read permissions on the first file could cause this too.
I just had this problem because I had the same program scheduled twice at the same time. Stepping all over each other.
来源:https://stackoverflow.com/questions/30731390/files-are-copied-but-size-remains-0-kb-using-shutil-copy2