05-shutil module

shutil module

Senior files, folders, archive processing module

shutil.copyfileobj(fsrc, fdst[, length])

Description: copy the contents of the file to another file

import shutil
shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

shutil.copyfile (src, dst)

Description: Copy file

shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在

shutil.copymode (src, dst)

Description: Copy rights only. Content, groups, users remain unchanged

shutil.copymode('f1.log', 'f2.log') #目标文件必须存在

shutil.copystat (src, dst)

Note: Only copy status information, comprising: mode bits, atime, mtime, flags

shutil.copystat('f1.log', 'f2.log') #目标文件必须存在

shutil.copy (src, dst)

Description: Copy files and permissions

import shutil 
shutil.copy('f1.log', 'f2.log')

shutil.copy2 (src, dst)

Description: copying files and status information

import shutil 
shutil.copy2('f1.log', 'f2.log')

shutil.ignore_patterns(*patterns)

shutil.copytree(src, dst, symlinks=False, ignore=None)

Description: recursively to copy the folder

import shutil
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除 
import shutil

shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))

'''
通常的拷贝都把软连接拷贝成硬链接,即对待软连接来说,创建新的文件
'''

拷贝软连接

shutil.rmtree(path[, ignore_errors[, onerror]])

Note: to delete files recursively

import shutil 
shutil.rmtree('folder1')

shutil.move(src, dst)

Note: to move files recursively, which is similar mv command, in fact, renamed.

import shutil 
shutil.move('folder1', 'folder3')

shutil.make_archive(base_name, format,...)

Create a compressed package and return the file path, such as: zip, tar

Create a compressed package and return the file path, such as: zip, tar

  • base_name: archive file name, it can be compressed path. Just when the file name is saved to the current directory, or saved to a specified location,
    such as data_bak => to save the current path
    , such as: / tmp / data_bak => Save to / tmp /
  • format: compressed packet type, "zip", "tar", "bztar", "gztar"
  • root_dir: To compress the folder path (the current directory by default)
  • owner: the user, the current user default
  • group: group, by default the current group
  • logger: for logging, the object is usually logging.Logger
#将 /data 下的文件打包放置当前程序目录
import shutil
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
  
  
#将 /data下的文件打包放置 /tmp/目录
import shutil
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')

shutil compressed packet is processed and call ZipFile TarFile two modules to carry out, in detail:

import zipfile

# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()

zipfile压缩解压缩
import tarfile

# 压缩
>>> t=tarfile.open('/tmp/egon.tar','w')
>>> t.add('/test1/a.py',arcname='a.bak')
>>> t.add('/test1/b.py',arcname='b.bak')
>>> t.close()


# 解压
>>> t=tarfile.open('/tmp/egon.tar','r')
>>> t.extractall('/egon')
>>> t.close()

tarfile压缩解压缩

Guess you like

Origin www.cnblogs.com/zhuyouai/p/12602491.html