2 回答
TA贡献1836条经验 获得超13个赞
将文件生成名称制作成一个函数,以便您可以随时随地调用它,如下所示。
ROOT_DIR = r'C:/Users/user/'
BASE_DIR = r'documents'
BACKUP_DIR = r'path/to/backups/folder/'
BACKUP_SUFFIX = 'docs_backup'
def make_backup_path():
now = datetime.now()
date = now.strftime("%Y-%m-%d_%H-%M-%S")
backup_path = BACKUP_DIR + date + BACKUP_SUFFIX
return backup_path
@sched.scheduled_job('interval', hours=1)
def zip_method():
#Make the backup archive every one hour
shutil.make_archive(
make_backup_path(),
'zip',
ROOT_DIR,
BASE_DIR)
print(date)
print("I did a backup!")
#This is to make the backup when the computer starts.
shutil.make_archive(
make_backup_path(),
'zip',
ROOT_DIR,
BASE_DIR)
sched.start()
TA贡献1784条经验 获得超8个赞
考虑查找本地和全球范围。如果将创建日期的代码部分放在函数中,它会在每次调用时创建一个新日期。
#This is to make the backup when the computer starts.
shutil.make_archive(
backup_dir,
'zip',
root_dir,
base_dir)
@sched.scheduled_job('interval', hours=1)
def zip_method():
# Establish the key variables
now = datetime.now()
date = now.strftime("%Y-%m-%d_%H-%M-%S")
backup_dir = r'path/to/backups/folder/' + date + 'docs_backup'
root_dir = r'C:/Users/user/'
base_dir = r'documents'
#Make the backup archive every one hour
shutil.make_archive(
backup_dir,
'zip',
root_dir,
base_dir)
print(date)
print("I did a backup!")
sched.start()
添加回答
举报
