I. INTRODUCTION
Shutil is an advanced file, folder, and compression package processing module.
Two. Use
Shutil.copyfileobj (FSRC, fdst[, length])
Copy the contents of a file to another file
| 123 |
importshutil shutil.copyfileobj(open(‘old.xml‘,‘r‘), open(‘new.xml‘, ‘w‘)) |
Shutil.copyfile (SRC, DST)
Copy files
| 1 |
shutil.copyfile(‘f1.log‘, ‘f2.log‘) |
Shutil.copymode (SRC, DST)
Copy permissions only. Content, group, user are not changed
| 1 |
shutil.copymode(‘f1.log‘, ‘f2.log‘) |
Shutil.copystat (SRC, DST)
Copy only the status information, including: mode bits, Atime, mtime, Flags
| 1 |
shutil.copystat(‘f1.log‘, ‘f2.log‘) |
Shutil.copy (SRC, DST)
Copying files and Permissions
| 1 |
shutil.copy(‘f1.log‘, ‘f2.log‘) |
Shutil.copy2 (SRC, DST)
Copying files and status information
| 1 |
shutil.copy2(‘f1.log‘, ‘f2.log‘) |
Shutil.ignore_patterns (*patterns)
Shutil.copytree (SRC, DST, Symlinks=false, Ignore=none)
Recursive go-to-copy folder
| 123 |
shutil.copytree(‘folder1‘, ‘folder2‘, ignore=shutil.ignore_patterns(‘*.pyc‘, ‘tmp*‘))shutil.copytree(‘f1‘, ‘f2‘, symlinks=True, ignore=shutil.ignore_patterns(‘*.pyc‘, ‘tmp*‘)) |
Shutil.rmtree (path[, ignore_errors[, OnError])
To delete a file recursively
| 1 |
shutil.rmtree(‘folder1‘) |
Shutil.move (SRC, DST)
To move a file recursively, it is like the MV command, which is actually renamed.
| 1 |
shutil.move(‘folder1‘, ‘folder3‘) |
Shutil.make_archive (base_name, Format,...)
Create a compressed package and return the file path, for example: Zip, tar
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. Only the file name is saved to the current directory, otherwise saved to the specified path,
For example: www + = Save to Current path
such as:/users/wupeiqi/www = Save to/users/wupeiqi/
- 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: Used to log logs, usually logging. Logger Object
| 12345678 |
#将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录importshutilret = shutil.make_archive("wwwwwwwwww", ‘gztar‘, root_dir=‘/Users/wupeiqi/Downloads/test‘) #将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录import shutilret =shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", ‘gztar‘, root_dir=‘/Users/wupeiqi/Downloads/test‘) |
Shutil Processing of compressed packets is done by calling the ZipFile and Tarfile two modules.
| 123456789101112 |
Import zipfile # compression z = zipfile. ZipFile ( ' laxi.zip ' , ' W ' ) z.write ( ' A.log ' ) z.write ( ' data.data ' ) z.close () &NBSP # unzip z = zipfile. ZipFile ( ' laxi.zip ' , ' R ' ) z.extractall () z.close () |
| 123456789101112 |
importtarfile # 压缩tar = tarfile.open(‘your.tar‘,‘w‘)tar.add(‘/Users/wupeiqi/PycharmProjects/bbs2.log‘, arcname=‘bbs2.log‘)tar.add(‘/Users/wupeiqi/PycharmProjects/cmdb.log‘, arcname=‘cmdb.log‘)tar.close() # 解压tar =tarfile.open(‘your.tar‘,‘r‘)tar.extractall() # 可设置解压地址tar.close() |
Python3 's Shutil module