我想使用Python时间表库或其他方法检查specfic文件夹中的文件数
我想每小时检查文件夹中的文件数 我尝试在python中使用调度库 但当它的计数改变时 我不能更新变量 求你了。给点建议我想使用Python时间表库或其他方法检查specfic文件夹中的文件数,python,schedule,Python,Schedule,我想每小时检查文件夹中的文件数 我尝试在python中使用调度库 但当它的计数改变时 我不能更新变量 求你了。给点建议 def running(before): os.chdir('\\\\111.11.11.11\\d\\Daily') files = list( os.listdir() ) before_count = before print('before_count_In : ',before_count) after = len(files)
def running(before):
os.chdir('\\\\111.11.11.11\\d\\Daily')
files = list( os.listdir() )
before_count = before
print('before_count_In : ',before_count)
after = len(files)
print('after_count_In : ', after)
if before_count != after:
shutil.copy(files[len(files)-1] , 'D:\\D\\test' ) *# This is , I can not updata before variable.*
print('hello log file is going')
############################################################
before = os.listdir()
before_count = len(before)
print('before_count_out : ',before_count)
sd.every(10).seconds.do(running , before_count)
while True:
sd.run_pending()
print('hi')
time.sleep(1)
您可以这样修改代码:
def running():
#os.chdir('\\\\111.11.11.11\\d\\Daily')
#files = list( os.listdir() )
#before_count = before
global before_count
before_count += 10
print('before_count_In : ',before_count)
after = len(files)
print('after_count_In : ', after)
if before_count != after:
shutil.copy(files[len(files)-1] , 'D:\\D\\test' ) # This is , I can not updata before variable.*
print('hello log file is going')
############################################################
before = os.listdir()
before_count = len(before)
global before_count
print('before_count_out : ',before_count)
sd.every(10).seconds.do(running)
while True:
sd.run_pending()
print('hi')
我编写了这个简单的函数,它计算文件的数量并将其打印出来
def size_dir(): #count number of files
out = open('dayly.txt','a')
fs = len(list(os.listdir('C:\\temp')))
return out.write(str(fs)+'\n') #add number of files in a new line
时间表设置为每30秒一次,当我在temp dir中创建文本文件时,路径是为windows设计的
schedule.every(30).seconds.do(size_dir)
while 1:
schedule.run_pending()
time.sleep(1)
我已经为你打印了输出