python使用7z解压软件备份文件脚本分享

要求安装:

1.python2.7z解压软件

backup_2.py

代码如下:

# filename: backup_2.py

”’backup files. version: v2, based on python 3.3 usage: backup.py -s:”dir1|dir2|…” -t:”target_dir” [-c:”comment”] -s: the source directories. -t: the target directory. -c: optional, any comment. examples: backup.py -s:”c:\\src\\f1|c:\\src\\f2|c:\\src\\f 3″ -t:”c:\\backup” backup.py -s:”c:\\src\\f 3″ -t:”c:\\backup” -c:”for sample””’

import osimport sysimport time

# read sys.argvprint(sys.argv)if len(sys.argv) < 2: print(__doc__) sys.exit()

source=[]target_dir=”comment=”for arg in sys.argv: if arg.startswith(‘-s:’): source=arg[3:].split(‘|’) print(source) elif arg.startswith(‘-t:’): target_dir=arg[3:]+os.sep print(target_dir) elif arg.startswith(‘-c:’): comment=arg[3:] print(comment)

for i in range(0, len(source)): source[i] = “\”” + source[i] + “\”” print(source[i])

# make the file name with the time and commenttoday=target_dir+time.strftime(‘%y%m%d’)now=time.strftime(‘%h%m%s’)

if len(comment)==0: # check if a comment was entered target=today+os.sep+now+’.7z’else: target=today+os.sep+now+’_’+\ comment.replace(‘ ‘,’_’)+’.7z’

# create the subdirectory by dayif not os.path.exists(today): os.mkdir(today) # make directory print(‘successfully created directory’,today)

# zip commandzip_command=”7z a %s %s” %(target,’ ‘.join(source))print(zip_command)

# run the backupif os.system(zip_command)==0: print(‘successful backup to’,target)else: print(‘backup failed’)