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

yipeiwu_com5年前Python基础

要求安装:

1.Python
2.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 os
import sys
import time

# Read sys.argv
print(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 comment
today=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 day
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print('Successfully created directory',today)

# zip command
zip_command="7z a %s %s" %(target,' '.join(source))
print(zip_command)

# Run the backup
if os.system(zip_command)==0:
    print('Successful backup to',target)
else:
    print('Backup FAILED')

相关文章

Python中的 is 和 == 以及字符串驻留机制详解

is 和 == 先了解下官方文档中关于 is 和 == 的概念。is 表示的是对象标示符(object identity),而 == 表示的是相等(equality);is 的作用是用...

python读取word 中指定位置的表格及表格数据

python读取word 中指定位置的表格及表格数据

1.Word文档如下: 2.代码 # -*- coding: UTF-8 -*- from docx import Document def readSpecTable(filen...

Django 大文件下载实现过程解析

django提供文件下载时,若果文件较小,解决办法是先将要传送的内容全生成在内存中,然后再一次性传入Response对象中: def simple_file_download(req...

Python pycharm 同时加载多个项目的方法

Python pycharm 同时加载多个项目的方法

在pycharm中只能一个项目存在,想打开另一个项目只能建一个新窗口或者把当前窗口覆盖掉。 在pycharm中其实可以同时打开多个项目: 1、file->setting->p...

Python3中的json模块使用详解

1. 概述 JSON (JavaScript Object Notation)是一种使用广泛的轻量数据格式. Python标准库中的json模块提供了JSON数据的处理功能. Pyt...