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')

相关文章

pytorch中如何使用DataLoader对数据集进行批处理的方法

pytorch中如何使用DataLoader对数据集进行批处理的方法

最近搞了搞minist手写数据集的神经网络搭建,一个数据集里面很多个数据,不能一次喂入,所以需要分成一小块一小块喂入搭建好的网络。 pytorch中有很方便的dataloader函数来方...

详解Python设计模式编程中观察者模式与策略模式的运用

观察者模式 观察者模式:又叫发布订阅模式,定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态发生变化时,会通知所有观察者对象,是他们能自动更新自己。...

Python 根据日志级别打印不同颜色的日志的方法示例

本文介绍了Python 根据日志级别打印不同颜色的日志的方法示例,分享给大家,具体如下: # -*-coding:UTF-8-*- import logging import os...

python实现文本去重且不打乱原本顺序

代码也是在网上找的,效率挺不错的,特别适合字典文件的去重 #coding=utf-8 import sys def open_txt(): #打开TXT文本写入数组 try: xxx...

Python时间戳使用和相互转换详解

本文实例为大家分享了Python时间戳使用和相互转换的具体代码,供大家参考,具体内容如下 1.将字符串的时间转换为时间戳 方法:    &nbs...