Python文件备份

MrDing_CoolPig 2018-02-09 10:05:22
代码:

import os
import time
import zipfile

source = ['D:\\Smart_Husband_Sen\\Some_words\\Python\\Byte\\Source']
target_dir = 'D:\\Smart_Husband_Sen\\Some_words\\Python\\Byte\\Backup'
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
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("Succesful backup to ",target)
else:
print('Backup Failed')

运行结果:
C:\Users\D_S17\AppData\Local\Programs\Python\Python36\python.exe D:/Smart_Husband_Sen/Python/跟着大佬学Python/备份.py
Zip command is :
zip - r D:\Smart_Husband_Sen\Some_words\Python\Byte\Backup\20180209214643.zipD:\Smart_Husband_Sen\Some_words\Python\Byte\Source
RUNNING:
(结果是乱码)

Backup Failed

...全文
1284 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2018-02-09
  • 打赏
  • 举报
回复

zip_command = 'zip - r {0} {1}'.format(target,''.join(source))
加个空格

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧