我只想复制前 50 个文件。我知道如何复制文件,但如何只复制前 50 个文件?这是我用于复制文件的代码。它复制文件夹中存在的所有文件。我只想复制前 50 个文件。
import sys, os, time, shutil
print time.ctime()
path = "C:\\temp"
files = os.listdir(path)
print len(files)
if len(files)<=0:
print'No Files Present'
else:
files.sort()
fileobj = open("log.txt",'a')
for eachfilename in files:
src = path+'\\'+eachfilename
temp = "C:\\Backup\\" +eachfilename
dst = "C:\\Dest\\" +eachfilename
shutil.copy(src,temp)
retrn_val = shutil.move(src, dst)
print retrn_val
print "File moved:",eachfilename
if retrn_val:
fileobj.write(eachfilename+','+'moved Sucessfully'+'\n')
else:
fileobj.write(eachfilename+','+'failed to move'+'\n')
print time.ctime()
有没有指定要复制的文件数量的功能?