Python常用模块-shutil高级文件处理
shutil是Python高级的 文件、文件夹、压缩包 处理模块
- 1 shutil.copyfileobj将文件内容覆盖拷贝到另一个文件中
shutil.copyfileobj (fsrc, fdst[, length])(copyfileobj方法只会拷贝文件内容)
将文件内容拷贝到另一个文件中f1 = open('file1','r') print(f1.read())f1.close()f2 = open('file2','r') print(f2.read())f2.close()import shutilshutil.copyfileobj(open('file1','r'),open('file2','w'))f2 = open('file2','r') print(f2.read())f2.close()print('----------copy覆盖之后验证----------')f1 = open('file1','r') print(f1.read())f1.close()f2 = open('file2','r') print(f2.read())f2.close() ###################### >>> a1 = open('file1','r') >>> a2 = open('file2','w') >>> shutil.copyfileobj(a1,a2) >>> a1.close() >>> a2.close() >>> f = open('file2','r') >>> f.read() '111 222\n333\n' >>>
- 2 shutil.copyfile(文件1,文件2):不用打开文件,直接用文件名进行覆盖copy。
只拷贝文件内容,不拷贝文件信息等
print(open('file1','r').read())print(open('3.txt','r').read())import shutilshutil.copyfile("file1","3.txt")print(open('3.txt','r').read())>>> print(open('3.txt','r').read())Traceback (most recent call last): File "", line 1, in FileNotFoundError: [Errno 2] No such file or directory: '3.txt'>>> import shutil>>> shutil.copyfile("file1","3.txt")'3.txt'>>> print(open('3.txt','r').read())111 222333>>>>>> import os>>> import shutil>>> os.stat('file1')os.stat_result(st_mode=33279, st_ino=668017, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527668901, st_mtime=1527499757, st_ctime=1527668861)>>> oct(os.stat('file1').st_mode)[-3:]'777'>>> shutil.copyfile('file1','222.txt')'222.txt'>>> oct(os.stat('222.txt').st_mode)[-3:]'664'>>> open('222.txt','r').read()'111 222\n333\n'>>>
- 3 shutil.copy(src, dst) 拷贝文件和权限(不包括状态信息)
>>> oct(os.stat('file1').st_mode)[-3:]'777'>>> shutil.copy('file1','333.txt') '333.txt'>>> open('333.txt','r').read() '111 222\n333\n'>>> oct(os.stat('333.txt').st_mode)[-3:]'777'>>>>>> os.stat('file1')os.stat_result(st_mode=33279, st_ino=668017, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527668901, st_mtime=1527499757, st_ctime=1527668861)>>> os.stat('333.txt') os.stat_result(st_mode=33279, st_ino=671442, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527672606, st_mtime=1527672594, st_ctime=1527672594)>>>
- 4 shutil.copymode(src, dst) 仅拷贝权限。内容、组、用户均不变
前提是dst文件存在,不然报错
>>> import os>>> import shutil>>> shutil.copymode('file1','444.txt')Traceback (most recent call last): File "", line 1, in File "/usr/local/python3.6.0/lib/python3.6/shutil.py", line 138, in copymode chmod_func(dst, stat.S_IMODE(st.st_mode))FileNotFoundError: [Errno 2] No such file or directory: '444.txt'>>> open('444.txt','w')<_io.TextIOWrapper name='444.txt' mode='w' encoding='UTF-8'>>>> shutil.copymode('file1','444.txt')>>> open('file1','r').read() '111 222\n333\n'>>> open('444.txt','r').read()''>>> os.stat('file1')os.stat_result(st_mode=33279, st_ino=668017, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527668901, st_mtime=1527499757, st_ctime=1527668861)>>> os.stat('444.txt') os.stat_result(st_mode=33279, st_ino=666566, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=0, st_atime=1527733771, st_mtime=1527733730, st_ctime=1527733759)>>>
- 5 shutil.copystat(src, dst) dst需要存在
仅拷贝状态的信息,即文件属性,包括:mode bits, atime, mtime, flags
>>> os.stat('file1')os.stat_result(st_mode=33279, st_ino=668017, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527668901, st_mtime=1527499757, st_ctime=1527668861)>>> os.stat('5.txt') os.stat_result(st_mode=33279, st_ino=666566, st_dev=51713, st_nlink=1, st_uid=505, st_gid=505, st_size=0, st_atime=1527733771, st_mtime=1527733730, st_ctime=1527733977)>>> open('5.txt','r').read() ''>>> open('file1','r').read() '111 222\n333\n'>>> shutil.copystat('file1','6.txt')Traceback (most recent call last): File "", line 1, in File "/usr/local/python3.6.0/lib/python3.6/shutil.py", line 195, in copystat follow_symlinks=follow)FileNotFoundError: [Errno 2] No such file or directory>>> shutil.copystat('file1','5.txt')Traceback (most recent call last): File " ", line 1, in File "/usr/local/python3.6.0/lib/python3.6/shutil.py", line 195, in copystat follow_symlinks=follow)PermissionError: [Errno 1] Operation not permitted>>> shutil.copystat('file1','5.txt')>>> open('5.txt','r').read() ''>>> os.stat('file1') os.stat_result(st_mode=33279, st_ino=668017, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527668901, st_mtime=1527499757, st_ctime=1527668861)>>> open('5.txt','r').read() ''>>> os.stat('5.txt') os.stat_result(st_mode=33279, st_ino=666566, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=0, st_atime=1527734058, st_mtime=1527499757, st_ctime=1527734052)>>>
- 6 shutil.copy2(src, dst)拷贝文件和状态信息
>>> shutil.copy2('file1','6.txt') '6.txt'>>> os.stat('file1') os.stat_result(st_mode=33279, st_ino=668017, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527668901, st_mtime=1527499757, st_ctime=1527668861)>>> os.stat('6.txt')os.stat_result(st_mode=33279, st_ino=666567, st_dev=51713, st_nlink=1, st_uid=503, st_gid=503, st_size=12, st_atime=1527668901, st_mtime=1527499757, st_ctime=1527734189)>>> open('6.txt','r').read()'111 222\n333\n'>>> open('file1','r').read() '111 222\n333\n'>>>
- 7 shutil.copytree(src, dst, symlinks=False, ignore=None)递归的去拷贝文件夹
- 8 shutil.ignore_patterns(*patterns)(忽略哪个文件,有选择性的拷贝)
shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)symlinks 符号链接
>>> import shutil >>> os.stat('bbb')os.stat_result(st_mode=16893, st_ino=668009, st_dev=51713, st_nlink=2, st_uid=503, st_gid=503, st_size=4096, st_atime=1527734521, st_mtime=1527734426, st_ctime=1527734426)>>> os.listdir('bbb')['3.txt', 's.py', '111', '222.txt']>>> shutil.copytree('bbb','qqq',ignore=shutil.ignore_patterns('*.py'))'qqq'>>> os.stat('qqq')os.stat_result(st_mode=16893, st_ino=666572, st_dev=51713, st_nlink=2, st_uid=503, st_gid=503, st_size=4096, st_atime=1527734521, st_mtime=1527734426, st_ctime=1527734913)>>> os.listdir('qqq')['3.txt', '111', '222.txt']>>>
- 9 shutil.rmtree(path[, ignore_errors[, onerror]]) 递归的去删除文件
>>> import shutil>>> shutil.rmtree('eee')
- 10 shutil.move(src, dst)递归的去移动文件
改名或者以后到某目录下>>> shutil.move('ddd','qqq') 'qqq/ddd'>>> shutil.move('bbb','zzz')'zzz'>>>
- 11 shutil.make_archive(base_name, format,...)创建压缩包并返回文件路径,例如:zip、tar
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径 如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
logger: 用于记录日志,通常是logging.Logger对象
>>> shutil.make_archive('www','gztar',root_dir='/home/admin/wangxu/zzz',owner='rd')'/home/admin/wangxu/zzz/www.tar.gz'>>> import os>>> os.path.isfile('www.tar.gz')True>>> shutil.make_archive('zzz','gztar',root_dir='/home/admin/wangxu/zzz',owner='rd')'/home/admin/wangxu/zzz/zzz.tar.gz'>>> shutil.make_archive('.','gztar',root_dir='/home/admin/wangxu/zzz',owner='rd') '/home/admin/wangxu/zzz.tar.gz'>>>
- 1 2 shutil压缩包的处理是调用 ZipFile 和 TarFile 两个模块
zipfile压缩不会保留文件的状态信息,而tarfile会保留文件的状态信息
zipfile
>>> import zipfile >>> z = zipfile.ZipFile('qqq.zip','w')>>> z.write('5.txt')>>> z.write('qqq')>>> z.close()>>> import os>>> os.path.isfile('qqq.zip')True >>> z = zipfile.ZipFile('qqq.zip','r')>>> z.extractall() #解压地址>>> z.close()>>> os.path.isfile('5.txt')True>>>
tarfile
>>> import zipfile >>> z = zipfile.ZipFile('qqq.zip','w')>>> z.write('5.txt')>>> z.write('qqq')>>> z.close()>>> import os>>> os.path.isfile('qqq.zip')True >>> z = zipfile.ZipFile('qqq.zip','r')>>> z.extractall() #解压地址>>> z.close()>>> os.path.isfile('5.txt')True>>>tarfile>>> import tarfile >>> t = tarfile.open('src.tar','w')>>> t.add('qqq')>>> t.add('5.txt')>>> t.close()>>> import os>>> import shutil>>> shutil.rmtree('qqq')>>> os.remove('5.txt')>>> t = tarfile.open('src.tar','r')>>> t.extractall('/tmp') >>> t.close()>>> os.path.isfile('/tmp/5.txt')True>>> os.path.isfile('/tmp/qqq') False>>> os.path.isdir('/tmp/qqq') True>>>