Python 示例

简明Python教程 

例10.3 备份脚本——版本四

#!/usr/bin/python
# filename: backup.py

import os
import time

source = ['~/test','~/build.txt']

target_dir = '/home/rhu/'

today = target_dir + time.strftime('%Y%m%d')

now = time.strftime('%H%M%S')

comment = raw_input('Enter a comment: ')

if len(comment) == 0:
    target = today + os.sep + now + '.zip'
else:
    target = today + os.sep + now + '_' + comment.replace(' ', '_') + '.tar.gz'

if not os.path.exists(today):
    os.mkdir(today)
    print 'Success create directory', today

zip_command = "tar -zcvf %s %s" % (target, ' '.join(source))

print 'command is:', zip_command

if os.system(zip_command) == 0:
    print "Successful to:", target
else:
    print 'Backup FAILED'

还有一个命令

tar = 'tar -cvzf %s %s -X /home/swaroop/excludes.txt' % (target, ' '.join(srcdir))
-c  表示创建一个归档文件
-v 表示交互, 命令具有交互性
-z 表示使用gzip滤波
-f  表示强迫创建,如果存在同名文件,覆盖
-X 表示指定文件

以上有用内容,转载自

简明Python教程 

http://sebug.net/paper/python/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值