python - 代码练习 - 备份目录/文件(windos环境)
备份目录/文件
import zipfile
import os,datetime class beifen(object):
def __init__(self,files,new_files,compression=zipfile.ZIP_DEFLATED):
self.files = files
self.new_files = new_files
self.mode = ''
self.compression=compression
self.blo = self.has_file()
self.zhuluoji() def has_file(self):
if not os.path.exists(self.files):
raise EOFError("原文件不存在!")
else:
# print("源文件存在")
if os.path.isdir(self.files):
# print("源文件是目录")
return False
else:
# print("源文件是文件")
return True def has_new_files(self):
if not os.path.exists(self.new_files):
# print("目的地址文件不存在,直接备份")
self.mode = 'w'
else:
# print("目的地址文件存在,追加备份")
self.mode = 'a'
# self.new_files = datetime.datetime.now().strftime('%Y-%m-%d') + "-" +self.new_files+ '.zip'
dirname = os.path.dirname(self.new_files)
basename = os.path.basename(self.new_files)
self.new_files = dirname + r'\\' + datetime.datetime.now().strftime('%Y-%m-%d') +" "+ basename +'.zip'
print(self.new_files) def zipfile(self,blo):
with zipfile.ZipFile(self.new_files, self.mode, self.compression) as z:
print("正在执行备份操作.....请稍后.......")
if blo:
z.write(self.files)
else:
for i1,i2,i3 in os.walk(self.files):
for x in i3:
z.write(os.path.join(i1,x))
print("备份成功!") def zhuluoji(self):
self.has_new_files()
if self.blo:
self.zipfile(self.blo)
else:
self.zipfile(self.blo) if __name__=='__main__':
print(r"""
files = 路径\源文件名.文件类型 or 路径\源文件目录;
new_files = 绝对-相对路径\文件名;
""")
files = input("输入源文件路径")
new_files = input("输入目的文件路径")
bf = beifen(r"D:\aaa\JetbrainsCrack-2.8-release-enc.jar",r'D:\bbb\备份')
python - 代码练习 - 备份目录/文件(windos环境)的更多相关文章
- python之对指定目录文件夹的批量重命名
python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...
- Python +crontab定时备份目录发送邮件
公司有一台静态页面展示服务器仅供给客户展示我们做的项目,当时买的时候是最低配,也就是磁盘空间为20G的系统盘,考虑到代码量很小所以没有另加磁盘,后来项目多了,就写了个crontab 定时备份目录. 就 ...
- 忘带U盘了??别急!一行python代码即可搞定文件传输
近日发现了python一个很有趣的功能,今天在这里给大伙儿做一下分享 需求前提 1.想要拷贝电脑的文件到另一台电脑但是又没有U盘2.手机上想获取到存储在电脑的文件3.忘带U盘- 您也太丢三落四了吧,但 ...
- python打开一个本地目录文件路径
os.path.abspath()os 模块为 python 语言标准库中的 os 模块包含普遍的操作系统功能.主要用于操作本地目录文件.path.abspath()方法用于获取当前路径下的文件. 比 ...
- 如何在创建hive表格的python代码中导入外部文件
业务场景大概是这样的,我要对用户博文进行分词(这个步骤可以看这篇文章如何在hive调用python的时候使用第三方不存在的库-how to use external python library in ...
- vi,etc目录文件,环境变量,别名功能
1 vi命令使用技巧补充 1.1 如何快速编辑文本内容 yy ---快速复制文件内容 3yy ---复制三行内容 p ---快速粘贴文本内容 3p ---粘贴三行内容 dd ---快速删除文件内容 ...
- win7下用PyInstaller把Python代码打包成exe文件
2013-11-05 22:02:14| 1.安装 使用PyInstaller需要安装PyWin32. 下载与Python对应的PyInstaller版本,解压后就算安装好了. 例如,安装了PyI ...
- python代码打包成exe文件
1.准备工作 1.安装pywin32.pyinstaller 2.准备好ico文件 找一个png格式的图片,使用png2ico脚本生成包含以下6个尺寸的ico文件:128×128 64×64 48×4 ...
- 【python】引用其他目录文件
假设有 目录/A(a.py), 目录/B(b.py), 括号里是目录中的文件 在目录/A中编写a2.py,里面可以import a,但是不能import b 解决方法 import sys sys.p ...
随机推荐
- 网络流n题
近日好不容易在自救之路写完暑训遗留下来的网络流8题,在此回顾一下. Going Home POJ - 2195 题意:m要去H,一个H只能容纳一个m,一步一块钱,问最小花费. 思路:最小费用最大流的板 ...
- centos7.4+openvpn-2.4.4+easy-rsa-3.0
[生产环境物理机安装]openvpn2.4.4服务搭建,并可以正常运行 服务器内网:172.31.33.64 openvpn:10.8.0.0 本机ifconfig yum install epel- ...
- 序列化serialize与反序列化unserialize
有利于存储和传递value(除了resource类型外),却不会丢失其原有类型和结构. serialize序列化时会调用魔术方法__sleep(); unserialize反序列化时会调用魔术方法__ ...
- win7,win10系统激活工具下载
1.关于激活win10: 工具下载地址:链接:https://pan.baidu.com/s/1ZqYbXnp7P8EhE8m0BvVMBA 密码:gklw 这个工具可以激活office,win10 ...
- 【MSSQL】SqlServer中delete语句表别名的问题
1.一般情况下删除表数据的sql语句: delete from products 2.如果想给表起个别名再删除呢,就得像下面这样写了 delete products from products as ...
- python 计算机发展史,线程Process使用 for循环创建 2种传参方式 jion方法 __main__的解释
########################总结################## #一 操作系统的作用: 1:隐藏丑陋复杂的硬件接口,提供良好的抽象接口 2:管理.调度进程,并且将多个进程对硬 ...
- Sidetiq 定时任务
class SidekiqCreateMonthPlanWorker #定时自动生成下月计划 include Sidekiq::Worker include Sidetiq::Schedulable ...
- mysql一张表多个字段关联另一张表查询
如下:一张订单表多个字段关联用户表: 1.链表查询 SELECT cu.id AS 'id',cu.version AS 'version',cu.cid AS 'cid',cu.uid AS 'ui ...
- bzoj千题计划314:bzoj3238: [Ahoi2013]差异(后缀数组+st表+单调栈)
https://www.lydsy.com/JudgeOnline/problem.php?id=3238 跟 bzoj3879 差不多 #include<cstdio> #include ...
- SQL Server2012安装流程
今天手比较抽风,把原来的SQL Server给卸载了,卸载还卸了半天,真是…… 安装时找了好多教程,结果都不是很详细,然后准备自己摸索一下,把这个过程记录下来,供大家参考,如果有不当的地方,欢迎指正, ...