我有一个python代码,用于将文件夹从文件夹a传输到文件夹b 在该文件夹中,如果有多个文件(例如30个),我一次只需要传输5个文件, 以下是代码
#!/usr/bin/python
import os, sys, time
src_path = "/opt/tst1/"
dst_path = "/opt/tst1/consume/"
now = time.time()
cutoff = now - (5 * 60)
count = 5
files = os.listdir(src_path)
for f in files:
fn = src_path + f
if not os.path.isfile(fn):
continue
t = os.stat(fn)
c = t.st_ctime
if c > cutoff:
continue
# move the file
dfn = dst_path + f
os.rename(fn, dfn)
count -=1
if count == 0:
Break
它将文件夹的全部内容从1个文件夹复制到另一个文件夹,而不是当时只复制5个文件,是否需要添加任何文件
答案 0 :(得分:0)
此代码将一次发送5个文件,直到所有文件都耗尽
files = os.listdir(".")
while files:
print "COPY 5"
for i in range(5):
try:
next_file = files.pop() #get the next file if we can
except IndexError:
print "DONE!" #if we cant we are done
break
print next_file
do_something(next_file)
print "Resting"
time.sleep(however_long)