python备份脚本中zip_command = "rar a %s %s"%(target,' '.join(source))

整个脚本已经可以运行,只是有不理解的地方

脚本中对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')
阅读 2.6k
1 个回答

' '.join(source)仅仅只是将 source 的内容,通过空格拼接成字符串而已,例如:

>>> print ' '.join(s)
s aa

拼接完之后,就和 target 一起传入 "rar a %s %s" 进而替换成实际的命令,最后再给 os.system 执行。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题