创建文件备份
一个简单的例子,A byte of Python的例子,利用系统自带的命令创建某些文件的备份,放入指定的目录中
import os import time source = ['/home/dat/python/test1','/home/dat/python/test2'] target_dir = '/home/dat/python/' 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 else: target = today+os.sep+now+'_'+comment.replace(' ','_')+'.zip' if not os.path.exists(today): os.mkdir(today) print 'successfully created directory',today tar_command = 'tar -cvzf %s %s -X /home/dat/python/excludes.txt' % (target, ' '.join(source)) if os.system(tar_command)==0: print 'Success back to',target else: print 'Back up Failed'