zoukankan      html  css  js  c++  java
  • python文件备份脚本

    import os
    import time

    source = ['D:\MyDrivershotfix']   #这里可以用自然字符串表示r',因为windows下的分隔符
    与python的有冲突,所以需要转义字符

    # 2. 备份文件到目标路径
    target_dir = 'F:\DMDownLoad\'  #这里的末尾一定不要丢分隔符,否者创建的文件会在F:目录下,
    而不会在DMDownload目录下

    # 3. The files are backed up into a zip file.
    # 4. The current day is the name of the subdirectory in the main directory
    today = target_dir + time.strftime('%Y%m%d') #time.strftime表示对当前时间的调用,括号内为参数设定
    # The current time is the name of the zip archive
    now = time.strftime('%H%M%S')

    # Take a comment from the user to create the name of the zip file
    comment = raw_input('Enter a comment -->')
    if len(comment)==0: 
        target = today+os.sep+now+'.zip'  
    #os.sep表示目录符号,windows下是\,linux下是/,mac下是:,这里为了保证移植性,
    所以os.sep会根据系统给出分隔符
    else:
        target = today+os.sep+now+'_'+
                 comment.replace(' ','_')+'.zip'
        # Notice the backslash!

    # Create the subdirectory if it isn't already there
    if not os.path.exists(today):
        os.mkdir(today)  # make directory
        print('Successfully created directory', today)

    # 5. 用winrar的rar命令压缩文件,但首先要安装有winrar且设置winrar到环境变量的路径path中
    zip_command = "rar a %s %s" %(target,''.join(source))
    #这行命令之前的所有target   、target_dir、today这些都是字符串,只有在
    这个命令和os.makedir中才是真正的表示路径

    # Run the backup
    #设置winrar到path环境中,这里已经手动添加了,如果没有去掉#号
    #os.system('set Path=%Path%;C:Program FilesWinRAR')
    if os.system(zip_command)==0:
        print'Successful backup to', target
    else:
        print'Backup FAILED'

  • 相关阅读:
    优化 Markdown 在 Notepad++ 中的使用体验
    error C1128: 节数超过对象文件格式限制: 请使用 /bigobj 进行编译
    Git: 代码冲突常见解决方法
    fatal error c1001 编译器中发生内部错误 OpenMesh6.3
    error C2448 函数样式初始值设定项类似函数定义
    VS Code 配置Python
    15分钟掌握 Git
    notepad++快捷键
    【Python笔记】第4章 操作列表
    【MySQL】MySQL按中文排序
  • 原文地址:https://www.cnblogs.com/rick52o/p/7209414.html
Copyright © 2011-2022 走看看