shutil模块 + shelve模块 二合一版
其他的看我前面的博客 import shutil # 将文件内容拷贝到另一个文件
with open('old.xml','r') as read_f,open('new.xml', 'w') as write_f:
shutil.copyfileobj(read_f,write_f) # 将文件打包到当前程序所在的目录,可以在打包文件前加上需要打包到的目录,压缩包会打包到该目录。
shutil.make_archive("data_bak", 'gztar', root_dir='D:\SH_fullstack_s2\day04') import tarfile
t=tarfile.open('data_bak.tar.gz','r')
t.extractall('D:\SH_fullstack_s2\day20\dir') # 解压到指定目录
t.close()
shutil.copymode(src, dst)
# 仅拷贝权限。内容、组、用户均不变
shutil.copymode('f1.log', 'f2.log') # 目标文件必须存在 shutil.copystat(src, dst)
# 仅拷贝状态的信息,包括:mode,bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log') # 目标文件必须存在
# 拷贝文件
shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在 shelve序列化模块 shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型。
import shelve
dic1={'pwd':'alex3714','age':18,'sex':'male'}
dic2={'pwd':'alex3715','age':73,'sex':'male'}
d=shelve.open('db.txt',writeback=True) # writeback 写回
print(d) # d就是一个对象 <shelve.DbfilenameShelf object at 0x00000192FD9F0A90>
d['egon']=dic1 # 相当于序列化
d['alex']=dic2
d['egon']['age']=19
print(d['egon']) # 相当于反序列化 {'pwd': 'alex3714', 'age': 19, 'sex': 'male'}
d.close()
shutil模块 + shelve模块 二合一版的更多相关文章
- Python模块-shelve模块
shelve模块也是用来序列化的,可以持久化任何pickle可支持的python数据格式,比pickle好用,也是python专属,可以dump多次数据,也可以直接修改数据 序列化 # -*- cod ...
- 序列化模块--json模块--pickle模块-shelve模块
什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传播到远程,因为硬盘或网络传输时只能接受bytes 例: 把内存数据 转成字符 # data ={# 'roles ...
- python 之 random 模块、 shutil 模块、shelve模块、 xml模块
6.12 random 模块 print(random.random()) (0,1)----float 大于0且小于1之间的小数 print(random.randint(1,3)) [1,3] 大 ...
- python 闭包,装饰器,random,os,sys,shutil,shelve,ConfigParser,hashlib模块
闭包 def make_arerage(): l1 = [] def average(price): l1.append(price) total = sum(l1) return total/len ...
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
- python模块(shelve,xml,configparser,hashlib,logging)
1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...
- logging模块、sys模块、shelve模块
一.logging模块 1.logging模块就是用于记录日志的,日志就是记录某个时间点,发生的事情. 2.记录日志是为了日后来复查,提取有用的信息. 3.如何去记录日志:可以直接打开文件,记录信息, ...
- python基础--json,pickle和shelve模块
一.JSON &pickle 用于序列化的两个模块 json,用于字符串 和 python数据类型间进行转换 字符串必须是双引号,不能是单引号 pickle,用于python特有的类型 和 ...
- python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则
python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess ...
随机推荐
- IDEA失效的解决办法
1.根据下图进行操作即可解决
- 2016/2/26 <marquee></marquee>实现多种滚动效果
页面的自动滚动效果,可由javascript来实现,但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标记 ...
- hdfs对namenode format 之后 应该首先检查内存消耗情况,以判断是否支持开启yarn
http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-common/yarn-default.xml 3.0.0 yarn.sc ...
- MongoDB相关的一些技术文章
安装 win7下安装和配置MongoDB的总结---阿冬专栏
- XMU 1608 nc与加法进位 【二分】
1608: nc与加法进位 Time Limit: 2000 MS Memory Limit: 128 MBSubmit: 29 Solved: 27[Submit][Status][Web Bo ...
- HTTP传输二进制初探
[转]HTTP传输二进制初探 http://www.51testing.com/?uid-390472-action-viewspace-itemid-233993 [转]HTTP传输二进制初探 上一 ...
- ThreadLocal工具类的使用(隔离思想)
ThreadLocal不是用来解决共享对象的多线程访问问题的, 通过ThreadLocal的set()方法设置到线程的ThreadLocal.ThreadLocalMap里的是是线程自己要存储的对象, ...
- 洛谷P1527 矩阵乘法——二维树状数组+整体二分
题目:https://www.luogu.org/problemnew/show/P1527 整体二分,先把所有询问都存下来: 然后二分一个值,小于它的加到二维树状数组的前缀和里,判断一遍所有询问,就 ...
- 洛谷P2943 清理——DP
题目:https://www.luogu.org/problemnew/show/P2943 一眼看去就有个 n^2 的做法:f[i] = min{ f[j] + num( i - j ) * num ...
- 476. Number Complement(补数)
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...