所以我希望每两次备份一次这个文件,一次最多备份30个。一旦有30个备份,该功能将删除所有备份并重新开始(再次创建30个备份,每两秒一次)。
因为我创建了同一个文件的30个副本,所以我需要更改名称,以便python选择覆盖文件(main0.db,main1.db,main2.db,main3.db。 ..等等)。为此,我创建了一个附加到文件名(i)的变量,并在每次运行时增加1。
问题是因为我必须(?)对函数内部的变量进行decalre,每次运行时变量都会被重置,因此创建的唯一文件是main0.db。
有什么想法吗?
嘿,所以我希望每两次备份一次这个文件,一次最多备份30个。一旦有30个备份,该功能将删除所有备份并重新开始(再次创建30个备份,每两秒一次)。
因为我创建了同一个文件的30个副本,所以我需要更改名称,以便python选择覆盖文件(main0.db,main1.db,main2.db,main3.db。 ..等等)。为此,我创建了一个附加到文件名(i)的变量,并在每次运行时增加1。
问题是因为我必须(?)对函数内部的变量进行decalre,每次运行时变量都会被重置,因此创建的唯一文件是main0.db。
有什么想法吗?
import os
import glob
import threading
import shutil
def check():
i = 0
threading.Timer(2, check).start()
listPath = 'C:\Users\Billy\Desktop\New folder'
num_files = len([f for f in os.listdir(listPath)
if os.path.isfile(os.path.join(listPath, f))])
#counts all the files in the directory
if num_files < 30:
copyPath = 'C:\Users\Billy\Desktop\New folder\main%d.db' % i
i + 1
shutil.copy2('C:\main.db', copyPath)
else:
files = glob.glob('C:\Users\Billy\Desktop\New folder\*')
for f in files:
os.remove(f)
i = 0
#if theres less than 30 files, creates a copy of the file, iterates filename by 1
#else deletes all files in that folder, resets file name iterator to 0.
return
check()
由于