Advanced file, folder, compressed package processing module
1. shutil.copyfileobj(fsrc, fdst[, length]) Copy the contents of the file to another file
import shutil shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
2. shutil.copyfile(src, dst) copy file
shutil.copyfile( ' f1.log ' , ' f2.log ' ) #The target file does not need to exist
3. shutil.copymode(src, dst) only copy permissions. Contents, groups, users remain unchanged
shutil.copymode( ' f1.log ' , ' f2.log ' ) #The target file must exist
4. shutil.copystat(src, dst) only copies status information, including: mode bits, atime, mtime, flags
shutil.copystat( ' f1.log ' , ' f2.log ' ) #The target file must exist
5. shutil.copy(src, dst) Copy files and permissions
import shutil shutil.copy( ' f1.log ' , ' f2.log ' )
6. shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
recursively copy folders
import shutil shutil.copytree( ' folder1 ' , ' folder2 ' , ignore=shutil.ignore_patterns( ' *.pyc ' , ' tmp* ' )) #The target directory cannot exist. Note that the parent directory of the folder2 directory must have writable permissions, ignore means to exclude
7. shutil.rmtree(path[, ignore_errors[, onerror]]) recursively delete files
import shutil shutil.rmtree( ' folder1 ' )
8. shutil.move(src, dst) moves files recursively. It is similar to the mv command, but it is actually renaming.
import shutil shutil.move('folder1', 'folder3')
9、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: The file name of the compressed package, or the path of the compressed package. If it is only the file name, it will be saved to the current directory, otherwise it will be saved to the specified path.
Such as data_bak => save to the current path
such as: /tmp/data_bak => save to /tmp/
- format: the type of compressed package, "zip", "tar", "bztar", "gztar"
- root_dir: Folder path to be compressed (default current directory)
- owner: user, default current user
- group: group, default current group
- logger: used to record logs, usually a logging.Logger object
#Package the files under /data and place them in the current program directory import shutil ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data') #Package the files under /data and place them in the /tmp/ directory import shutil ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
10. The processing of the compressed package by shutil is carried out by calling two modules: ZipFile and TarFile. Details:
*zipfile compress & decompress
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()
*tarfile compression & decompression
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(