01 备份指定文件或目录

  • 2018-04-13
  • # coding=gbk
    '''
    注意:
    1 双引号括的是有空格的文件名,不要拿来括路径名
    2 路径要使用原始字符串r''
    3 有中文时要指明编码格式
    4 字符串连接列表用join函数
    '''
    import  os
    import time
    #本地时间转化为字符串格式时间
    current_day = time.strftime("%Y-%m-%d", time.localtime())
    current_time = time.strftime("%H-%M-%S", time.localtime())
    
    #用户给备份的文件输入注释
    comment = input("请输入注释:")
    if len(comment) == 0:
            target = r'D:\"OneDrive - hust.edu.cn"\desktop\备份\{}\{}.zip'.format(current_day,current_time)
    else:
            target = r'D:\"OneDrive - hust.edu.cn"\desktop\备份\{}\{}{}.zip'.format(current_day, current_time,comment.replace(" ", "_"))
    
    source = [r'D:\"OneDrive - hust.edu.cn"\desktop\byte-of-python-chinese-edition.pdf',
            r'D:\"OneDrive - hust.edu.cn"\desktop\京东.v']
    command = r'c:\"Program Files"\7-Zip\7z.exe a  {} {}'.format(target, "  ".join(source))
    
    if os.system(command) == 0:
            print("succeed")
    else:
            print("error")

     

posted on 2018-04-13 09:55  jkn1234  阅读(221)  评论(0编辑  收藏  举报