其实就是《A Byte of Python》里面一个例子程序,拿来练练手而已,没啥技术含量。

打包压缩程序用的是 7-Zip,安装后安装目录里有一个命令行版的 7z.exe,添加压缩文件的参数是 a;自动删除旧备份文件的方法很山寨很暴力,直接 listdir 备份目录下的文件,然后删除第一个,也就是最旧的一个,凑合吧。

Python语言: Codee#7002
01 #Python 备份 Fx 配置并自动删除旧备份
02 import os
03 import time
04
05 source = r’C:\FxProfiles’
06 target_dir = r’C:\FxBackup’
07 target = target_dir + os.sep + time.strftime(‘%Y%m%d%H%M%S’) + ‘.zip’
08 newzip = time.strftime(‘%Y%m%d%H%M%S’) + ‘.zip’
09 zip_command = "7z a %s %s" % (target, ‘ ‘.join(source))
10 oldzip = os.listdir(target_dir)
11 if newzip > oldzip:
12     os.remove(target_dir + os.sep + oldzip[0])
13     print ‘Del OK’
14
15 if os.system(zip_command) == 0:
16     print ‘Successful backup to’, target
17 else:
18     print ‘Backup FAILED’

山寨之极!不过还是玩的不亦乐乎,Python 很有搞头。

Tags: ,

Related posts

Tags: ,.
Home

No Comments Now!

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (required),(will not be published)
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://hanghang.name),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.