python学习:备份文档并压缩为zip格式
import os
import time source = ['/root/notes'] target_dir = '/root/backup' if not os.path.exists(target_dir):
os.mkdir(target_dir) today = target_dir+os.sep+time.strftime('%Y%m%d') now = time.strftime('%H%M%S') target=today+os.sep+now+'.zip' if not os.path.exists(today):
os.mkdir(today)
print('Successfully created directory',today) 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('Successful backup to', target)
else:
print('Backup Failed')
执行结果:
[root@localhost ~]# python backup.py
('Successfully created directory', '/root/backup/20170918')
Zip command is:
zip -r /root/backup/20170918/232132.zip /root/notes
Running:
adding: root/notes/ (stored 0%)
adding: root/notes/meminfo.py (deflated 41%)
adding: root/notes/__init__.py (stored 0%)
adding: root/notes/zidianpaixu.py (deflated 45%)
adding: root/notes/16.py (deflated 42%)
adding: root/notes/5.py (deflated 26%)
adding: root/notes/dmi.py (deflated 50%)
adding: root/notes/collect_info.py (deflated 68%)
adding: root/notes/dmi2.py (deflated 50%)
adding: root/notes/12.py (deflated 25%)
adding: root/notes/printpid2.py (deflated 34%)
adding: root/notes/11.py (deflated 11%)
adding: root/notes/8.py (deflated 57%)
adding: root/notes/printpid.py (deflated 41%)
adding: root/notes/10.py (deflated 7%)
adding: root/notes/ifconfig.py (deflated 39%)
adding: root/notes/13_ip.py (deflated 53%)
adding: root/notes/13.py (deflated 20%)
adding: root/notes/wc.py (deflated 31%)
adding: root/notes/4.py (deflated 32%)
adding: root/notes/15.py (deflated 39%)
('Successful backup to', '/root/backup/20170918/232132.zip')
python学习:备份文档并压缩为zip格式的更多相关文章
- Python中定义文档字符串__doc__需要注意格式对齐的处理
Python中的文档字符串是个很不错的提升代码交付质量.编写文档方便的特征,但是需要注意在使用文档字符串时,将文档字符串标识的引号对必须遵守缩进的规则,否则Python语法检查时会无法通过,而引号内的 ...
- python开发_tarfile_文档归档压缩|解压缩
''' python中的tarfile模块实现文档的归档压缩和解压缩 功能: 把工作空间下面的所有文件,打包生成一个tar文件 同时提供一个方法把该tar文件中的一些文件解压缩到 指定的目录中 ''' ...
- python初步学习-查看文档及数据类型转换
学会查看文档 python最权威的文档就是官方文档,因此,我们要学会查看官方文档.而在python解释器中,help() 命令就可以很权威的查看官方文档. 因此,我们要学会使用 help() 命令. ...
- 《Java开发学习大纲文档》V7.0
<Java开发学习大纲文档>V7.0简介: 本文档是根据企业开发所需要掌握的知识点大纲进行总结汇编,是Java开发工程师必备知识体系,系统化学习针对性非常强,逻辑分析能力非常清晰;技术方面 ...
- centos 文档的压缩和打包 gzip,bzip2,xz,zip,unzip,tar,tgz 第九节课
centos 文档的压缩和打包 gzip,bzip2,xz,zip,unzip,tar,tgz 第九节课 SAS盘可以支持热插拔,看机器 tar.zip.tar -czvf 不会动源文件,gz ...
- 用Python操作excel文档
使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档.如果大家有人不知道Excel的话,那么建议先学一学office办公基础.这里想要操作Excel,必须安装一个Pytho ...
- 使用Python操作Excel文档(一)
Python | 使用Python操作Excel文档(一) 0 前言 在阅读本文之前,请确保您已满足或可能满足以下条件: 请确保您具备基本的Python编程能力. 请确保您会使用Excel. 请确保您 ...
- python实用小技能分享,教你如何使用 Python 将 pdf 文档进行 加密 解密
上次说了怎么将word转换为pdf格式 及 实现批量将word转换为pdf格式(点击这里),这次我又get到一个新技能–使用 Python 将 pdf 文档进行 加密 解密,哈哈哈 希望帮到更多人! ...
- Python读取word文档内容
1,利用python读取纯文字的word文档,读取段落和段落里的文字. 先读取段落,代码如下: 1 ''' 2 #利用python读取word文档,先读取段落 3 ''' 4 #导入所需库 5 fro ...
随机推荐
- 在Intellij idea 2017中运行tomcat 8.5
前提:jdk,tomcat都已安装完,环境变量该配置的都配置了,tomcat localhost:xx 能登上去 首先新建web小项目 new project next 为项目命个名 finis ...
- angular4 中自定义pagination组件
你用Angular 吗? 一.介绍 一个基于angular4 开发的可以分页的组件.组件的好处就是可以复用,复用.....作为一个前端码农,开始的分页功能实现是我用jquery写的,其他同事用的时候都 ...
- Hystrix-request cache(请求缓存)
开启请求缓存 请求缓存在run()和construce()执行之前生效,所以可以有效减少不必要的线程开销.你可以通过实现getCachekey()方法来开启请求缓存. package org.hope ...
- android EditText与TextView几个常用的属性
android:maxLength="100"输入框最多输入的字数. android:maxEms="10"每行最多输入字符个数 android:textcol ...
- 【转】awk内置变量
awk是个优秀文本处理工具,可以说是一门程序设计语言.下面是awk内置变量. 一.内置变量表 属性 说明 $0 当前记录(作为单个变量) $1~$n 当前记录的第n个字段,字段间由FS分隔 FS 输入 ...
- Ajax发送数据
- Javascript的异步和回调
介绍JavaScript的一些同步.异步.单线程多线程,回调基本概念:https://segmentfault.com/a/1190000002999668
- Git多帐号配置,管理多个SSH
查看自己所有的SSH-Key $ cd ~/.ssh $ ls id_rsa id_rsa.pub known_hosts 如果你已经创建过git账号那你可能和我一样会看到只有一个SSH-Key, 这 ...
- Spring MVC的DispatcherServlet
Spring MVC提供了一个名为org.springframework.web.servlet.DispatcherServlet的Selvet充当前端控制器,所有的请求驱动都围绕这个Dispatc ...
- 本地工程引入maven工程的配置方式
一.准备 IDE: IntelliJ IDEA 2016.3.1 maven: 3.5.2 JDK: 1.8 操作系统: Window 7 二.配置 1. maven 3.5 下载地址:http:// ...