2 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊
將文件生成名稱制作成一個(gè)函數(shù),以便您可以隨時(shí)隨地調(diào)用它,如下所示。
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貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
考慮查找本地和全球范圍。如果將創(chuàng)建日期的代碼部分放在函數(shù)中,它會(huì)在每次調(diào)用時(shí)創(chuàng)建一個(gè)新日期。
#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()
添加回答
舉報(bào)