import osimport time#这里是需要文件所在的位置source=['"C:\\My Documents"',"C:\\Code"]#转换完成之后放到的文件目录target_dir="E:\\Backup"#判断有没有该文件,没有就创建一个if not os.path.exists(target_dir): os.mkdir(target_dir)#os.sep:取代操作系统特定的路径分隔符today=target_dir + os.sep +time.strftime("%Y%m%d")now = time.strftime("%H%M%S")target=today+os.sep+now+".zip"if not os.path.exists(today): os.mkdir(today) print("Successful created directory",today)#执行压缩的命令zip_command='zip -r {0} {1}'.format(target,' '.join(source))print("Zip command is:")print(zip_command)print("Running:")if os.system(zip_command)==0: print("Successful backup to",target)else: print("Backup FAILED")