Advanced files, folders, Compression pack processing modules
Shutil.copyfileobj (FSRC, fdst[, length])
Copy the contents of the file to another file:
# Source Code def copyfileobj (fsrc, FDST, length=16*1024): "" "copy data from File-like object FSRC to File-like object fdst"" And while 1: = fsrc.read (length) if not buf: Break fdst.write (BUF)
# Copying Files Import = open (' settings.py'rb'= open (' aaa.py'wb') shutil.copyfileobj (f, F1, length =20) f.close () f1.close ()
Shutil.copyfile (SRC, DST)
Copy files
#Source IdefCopyFile (SRC, DST):"""Copy data from SRC to DST""" if_samefile (SRC, DST):RaiseError ("'%s ' and '%s ' are the same file"%(SRC, DST)) forFninch[SRC, DST]:Try: St=Os.stat (FN)exceptOSError:#File Most likely does not exist Pass Else: #XXX What is the other special files? (Sockets, devices ...) ifStat. S_isfifo (st.st_mode):RaiseSpecialfileerror ("'%s ' is a named pipe"%fn) with open (SRC,'RB') as Fsrc:with open (DST,'WB') as Fdst:copyfileobj (FSRC, FDST)
Shutil.copymode (SRC, DST)
Copy only permissions, content, group, user are not changed
# Source Code def Copymode (SRC, DST): """ Copy mode bits from src to DST """ if ' chmod ' ): = os.stat (src) = Stat. S_imode (St.st_mode) os.chmod (DST, mode)
Example:
#The first step is to view the file permissions#llTotal dosage 16DRWXRWXR-X 2 huangxm huangxm 4096 March 24 14:32./DRWXRWXR-X 6 HUANGXM huangxm 4096 March 24 14:32. /-rw-rw-r--1 Huangxm HUANGXM 49 March 24 14:32Aaa.txt-rw-r--r--1 root root 29 March 24 14:32Bbb.txt#in the second step, create a new ccc.py and write the following code:#!/usr/bin/env Python3ImportShutil#Copy the file permissions of Bbb.txt to Aaa.txtShutil.copymode ('Bbb.txt','Aaa.txt')#python3 ccc.py Code Execution#Step Three: View file permissions#ls-lTotal dosage 12-rw-r--r--1 huangxm huangxm 49 March 24 14:32Aaa.txt-rw-r--r--1 root root 29 March 24 14:32Bbb.txt-rw-r--r--1 root root 77 March 14:35 ccc.py
Shutil.copystat (SRC, DST)
Copy status information, including: mode bits, Atime, mtime, Flags
#Source CodedefCopystat (SRC, DST):"""Copy All stat info (mode bits, Atime, mtime, flags) from SRC to DST"""St=os.stat (src) mode=Stat. S_imode (St.st_mode)ifHasattr (OS,'Utime'): Os.utime (DST, (St.st_atime, st.st_mtime))ifHasattr (OS,'chmod'): Os.chmod (DST, mode)ifHasattr (OS,'Chflags') andHasattr (St,'St_flags'): Try: Os.chflags (DST, st.st_flags)exceptOSError, why: forErrinch 'Eopnotsupp','Enotsup': ifHasattr (errno, err) andWhy.errno = =getattr (errno, err): Break Else: Raise
Shutil.copy (SRC, DST)
Copying files and Permissions
# Source Code def copy (SRC, DST): """ Copy data and Mode bits ("cp src DST"). The destination is a directory. """ if Os.path.isdir (DST): = Os.path.join (DST, Os.path.basename (SRC)) copyfile (src, DST) Copymode (src, DST)
Shutil.copy2 (SRC, DST)
Copying files and status information
# Source Code def copy2 (SRC, DST): """ Copy data and all stat info ("cp-p src DST"). The destination is a directory. """ if Os.path.isdir (DST): = Os.path.join (DST, Os.path.basename (SRC)) copyfile (src, DST) Copystat (src, DST)
Shutil.ignore_patterns (*patterns)
Shutil.copytree (SRC, DST, Symlinks=false, Ignore=none)
' D:/temp/python/zuoye/day6 ' # Zy_d6 folder for current directory, no need to create in advance ' Zy_d6 ' # to exclude files, you can use multiple, delimited support wildcard characters such as temp*ignore_file = shutil.ignore_patterns ('*.py' ) Shutil.copytree (SRC, DST, ignore=ignore_file)
Shutil.rmtree (PATH)
Recursively delete files
Shutil.move (SRC, DST)
Move files recursively
shutil.make_archive (base_name, Format, ...)
Create a compressed package and return the file path, for example: Zip, tar
Base_name: The file name of the compressed package, or the path to the compressed package. The file name is saved to the current path when the path is saved to the specified path
Format: Compressed package type, zip, tar, Bztar, Gztar
Root_dir: Folder path to compress (default current directory)
Owner: User, default Current user
Group: Groups, default current group
Logger: The user logs the log, usually the logging. Logger Object
# package The current directory in the current directory ret = shutil.make_archive ('dq'gztar' )
The return value is the compressed package name, and the value of RET in the above example is dq.tar.gz
# package The specified directory into the current directory ' D:/temp/python/zuoye/day6 ' = shutil.make_archive ('dq'gztar', root_dir= RootDir) Print (ret)
Shutil Processing of compressed packets is done by calling the ZipFile and Tarfile two modules, in detail:
ImportZipFile#Compressionz = zipfile. ZipFile ('Laxi.zip','W') Z.write ('A.log') Z.write ('Data.data') Z.close ()#Unzipz = zipfile. ZipFile ('Laxi.zip','R') Z.extractall () z.close ()
ImportTarfile#Compressiontar = Tarfile.open ('Your.tar','W') Tar.add ('/users/wupeiqi/pycharmprojects/bbs2.zip', arcname='Bbs2.zip') Tar.add ('/users/wupeiqi/pycharmprojects/cmdb.zip', arcname='Cmdb.zip') Tar.close ()#Unziptar = Tarfile.open ('Your.tar','R') Tar.extractall ()#can set the decompression addressTar.close ()
Python (6)-shutil module