整个脚本已经可以运行,只是有不理解的地方
脚本中对zip_command = "rar a %s %s"%(target,' '.join(source))的理解不到位,希望有理解的人能解释一下原理。
#!/usr/bin/python
# Filename:backupup_ver5.py
import os
import time
source = ['D:\\python-source\\nihao.txt']
target_dir = 'D:\\python-backup\\'
today = target_dir + time.strftime('%Y%m%d')
now = time.strftime('%H%M%S')
comment = input('Enter a comment -->')
if len(comment) == 0:
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' +\
comment.replace(' ','_') + '.zip'
if not os.path.exists(today):
os.mkdir(today)
print('Successfully create directory',today)
zip_command = "rar a %s %s"%(target,' '.join(source))
if os.system(zip_command) == 0:
print('Successful backup to',target)
else:
print('Backup FAILE')
' '.join(source)
仅仅只是将source
的内容,通过空格拼接成字符串而已,例如:拼接完之后,就和
target
一起传入"rar a %s %s"
进而替换成实际的命令,最后再给os.system
执行。