其实就是《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’
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: Coding, python
No Comments Now!
Be the first to comment on this entry.