第1章 模块
1.1 shutil 模块
![]()
1 import shutil
2 # 高级的 文件、文件夹、压缩包 处理模块
3
4 # 将文件内容拷贝到另一个文件中,可以部分内容
5 #
6 # shutil.copyfileobj(fsrc, fdst[, length])
7 # f = open("example.log")
8 # f2 = open("新的","w")
9 # shutil.copyfileobj(f,f2)
10 # shutil.copyfile("example.log","新的2")
11 # shutil.copymode("example.log","新的2") # copy权限
12
13 # shutil.copystat("example.log","新的2") # copy copy状态
14
15 # shutil.copy('example.log', '新的3') # 拷贝文件和权限
16 # shutil.copy2('example.log', '新的4') # 拷贝文件和状态信息
17 # shutil.copytree(r"D:\学习python\pyn\培训\day5\access.log.1",r"D:\学习python\pyn\培训\day6",ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
18
19 # shutil.rmtree() # 全删了
20
21
22 # shutil.make_archive(base_name, format,...) 打包
23
24 # shutil.make_archive("day5","zip","D:\学习python\pyn\自学\day5")
25
26
27
28 import zipfile
29
30 # 压缩
31 # z = zipfile.ZipFile('laxi.zip', 'w')
32 # z.write('example.log')
33 # z.write(r'D:\学习python\pyn\培训\day5\access.log.1',arcname="access.log.1")
34 # z.close()
35
36 # 解压
37 # z = zipfile.ZipFile('laxi.zip', 'r')
38 # z.extractall(members=["指定"],path=r"d:\\")
39 # z.extract("example.log")
40 # z.close()
41
42 # import tarfile
43 #
44 # # 压缩
45 # tar = tarfile.open('your.tar','w')
46 # tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
47 # tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
48 # tar.close()
49 #
50 # # 解压
51 # tar = tarfile.open('your.tar','r')
52 # tar.extractall() # 可设置解压地址
53 # tar.close()
view code