美文网首页
[python学习]day1疑问

[python学习]day1疑问

作者: starCoder | 来源:发表于2016-10-11 17:34 被阅读27次
#!/usr/bin/python
# Filename: backup_ver4.py
import os
import time
# 1. The files and directories to be backed up are specified in a list.
source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that
# 2. The backup must be stored in a main backup directory
target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using
# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')
# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
target = today + os.sep + now + '.zip'
else:
target = today + os.sep + now + '_' + \
comment.replace(' ', '_') + '.zip'
# Notice the backslash!
# Create the subdirectory if it isn't already there
if not os.path.exists(today):
os.mkdir(today) # make directory
print 'Successfully created directory', today
# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
print 'Successful backup to', target
else:
print 'Backup FAILED
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source)) 这句有些疑问不太懂

</p>

资料上解释是:</br>

zip命令有一些选项和参数。-q 选项用来表示 zip 命令安静地工作。-r 选项表示 zip 命令对目录递归地**工作,即它包括子目录以及子目录中的文件。两个选项可以组合成缩写形式-qr。选项后面跟着待创建的 zip 归档的名称,然后再是待备份的文件和目录列表。我们使用已经学习过的字符串 join 方法把 source 列表转换为字符串。

相关文章

网友评论

      本文标题:[python学习]day1疑问

      本文链接:https://www.haomeiwen.com/subject/abebyttx.html