Python標準庫系列之shutil模塊
The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os module.
對文件、文件夾、壓縮包進行處理的模塊。
官方文檔:https://docs.python.org/3.5/library/shutil.html
文件和目錄操作
shutil.copyfileobj(fsrc, fdst[, length])
將文件內容拷貝到另一個文件中
>>> import shutil # 循環讀取old.txt文件內容並寫入到new.txt文件當中 >>> shutil.copyfileobj(open('old.txt','r'), open('new.txt', 'w')) >>> import os # 查看複製過去的文件內容 >>> os.system("cat old.txt new.txt") old old 0
shutil.copyfile(src, dst, *, follow_symlinks=True)
拷貝整個文件,沒有第二個文件就創建,有就覆蓋
>>> os.system("cat old.txt new.txt") old new 0 >>> shutil.copyfile('old.txt', 'new.txt') # 把第二個文件內容給覆蓋了 >>> os.system("cat old.txt new.txt") old old 0
shutil.copymode(src, dst, *, follow_symlinks=True)
僅拷貝文件權限,文件的內容、組、用戶均不變
>>> os.system("ls -l old.txt new.txt") -rw-rw-rw- 1 ansheng ansheng 4 5月 26 15:54 new.txt -rw-rw-r-- 1 ansheng ansheng 4 5月 26 15:52 old.txt 0 >>> shutil.copymode('old.txt', 'new.txt') # 文件權限都變成644了 >>> os.system("ls -l old.txt new.txt") -rw-rw-r-- 1 ansheng ansheng 4 5月 26 15:54 new.txt -rw-rw-r-- 1 ansheng ansheng 4 5月 26 15:52 old.txt 0
shutil.copystat(src, dst, *, follow_symlinks=True)
拷貝文件狀態的信息,文件必須存在,不copy改動時間
>>> os.system("stat old.txt new.txt") 文件:'old.txt' 大小:4 塊:8 IO 塊:4096 普通文件 設備:801h/2049d Inode:1835014 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:53:09.813612241 +0800 最近更改:2016-05-26 15:52:54.830640166 +0800 最近改動:2016-05-26 15:52:54.830640166 +0800 創建時間:- 文件:'new.txt' 大小:4 塊:8 IO 塊:4096 普通文件 設備:801h/2049d Inode:1835024 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:56:22.540041783 +0800 最近更改:2016-05-26 15:54:24.244922722 +0800 最近改動:2016-05-26 15:55:38.353967649 +0800 創建時間:- 0 >>> shutil.copystat('old.txt', 'new.txt') >>> os.system("stat old.txt new.txt") 文件:'old.txt' 大小:4 塊:8 IO 塊:4096 普通文件 設備:801h/2049d Inode:1835014 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:53:09.813612241 +0800 最近更改:2016-05-26 15:52:54.830640166 +0800 最近改動:2016-05-26 15:52:54.830640166 +0800 創建時間:- 文件:'new.txt' 大小:4 塊:8 IO 塊:4096 普通文件 設備:801h/2049d Inode:1835024 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:53:09.813612000 +0800 最近更改:2016-05-26 15:52:54.830640000 +0800 最近改動:2016-05-26 15:56:48.765143115 +0800 創建時間:- 0
shutil.copy(src, dst, *, follow_symlinks=True)
拷貝文件和狀態信息,同樣不copy改動時間
>>> os.system("stat old.txt new.txt") 文件:'old.txt' 大小:4 塊:8 IO 塊:4096 普通文件 設備:801h/2049d Inode:1835014 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:53:09.813612241 +0800 最近更改:2016-05-26 15:52:54.830640166 +0800 最近改動:2016-05-26 15:52:54.830640166 +0800 創建時間:- 文件:'new.txt' 大小:0 塊:0 IO 塊:4096 普通空文件 設備:801h/2049d Inode:1835023 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:57:53.448632439 +0800 最近更改:2016-05-26 15:57:53.448632439 +0800 最近改動:2016-05-26 15:57:53.448632439 +0800 創建時間:- 0 >>> shutil.copy2('old.txt', 'new.txt') >>> os.system("stat old.txt new.txt") 文件:'old.txt' 大小:4 塊:8 IO 塊:4096 普通文件 設備:801h/2049d Inode:1835014 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:53:09.813612241 +0800 最近更改:2016-05-26 15:52:54.830640166 +0800 最近改動:2016-05-26 15:52:54.830640166 +0800 創建時間:- 文件:'new.txt' 大小:4 塊:8 IO 塊:4096 普通文件 設備:801h/2049d Inode:1835023 硬鏈接:1 權限:(0664/-rw-rw-r--) Uid:( 1000/ ansheng) Gid:( 1000/ ansheng) 最近訪問:2016-05-26 15:53:09.813612000 +0800 最近更改:2016-05-26 15:52:54.830640000 +0800 最近改動:2016-05-26 15:58:07.938760974 +0800 創建時間:- 0
shutil.ignore_patterns(*patterns)
This factory function creates a function that can be used as a callable for copytree()‘s ignore argument, ignoring files and directories that match one of the glob-style patterns provided. See the example below.
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)
遞歸的去拷貝文件夾
>> os.system("tree folder1") folder1 ├── dir ├── file.txt ├── sc.pyc ├── tmp └── vgauthsvclog.txt.0 -> /tmp/vgauthsvclog.txt.0 2 directories, 3 files 0 # folder2目錄必須不存在,symlinks=True只copy鏈接文件,如果等於False就copy源文件,ignore等於不copy的文件或者目錄 >>> shutil.copytree('folder1', 'folder2', symlinks=False, ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) >>> os.system("tree folder2") folder2 ├── dir ├── file.txt └── vgauthsvclog.txt.0 1 directory, 2 files 0
shutil.rmtree(path, ignore_errors=False, onerror=None)
遞歸的去刪除文件
>>> os.system("ls -d folder2") folder2 0 >>> shutil.rmtree('folder2') >>> os.system("ls -d folder2") ls: 無法訪問'folder2': 沒有那個文件或目錄 512
shutil.move(src, dst, copy_function=copy2)
遞歸的去移動文件,它類似mv命令,其實就是重命名。
>>> os.system("ls -ld folder1") drwxrwxr-x 4 ansheng ansheng 4096 5月 26 16:09 folder1 0 >>> shutil.move('folder1', 'folder3') >>> os.system("ls -ld folder1") ls: 無法訪問'folder1': 沒有那個文件或目錄 512 >>> os.system("ls -ld folder3") drwxrwxr-x 4 ansheng ansheng 4096 5月 26 16:09 folder3 0
shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]])
Create an archive file (such as zip or tar) and return its name.
>>> os.system("ls -dl folder3") drwxrwxr-x 4 ansheng ansheng 4096 5月 26 16:21 folder3 0 # /home/ansheng/folder3是保存的文件,gztar是後綴名,/home/ansheng/folder3是要打包的路徑 >>> shutil.make_archive("/home/ansheng/folder3", 'gztar', root_dir='/home/ansheng/folder3') # 返回文件打包放在那兒了 '/home/ansheng/folder3.tar.gz' >>> os.system("ls -dl /home/ansheng/folder3.tar.gz") -rw-rw-r-- 1 ansheng ansheng 263 5月 26 16:22 /home/ansheng/folder3.tar.gz 0
可選參數如下:
參數 | 說明 |
---|---|
base_name | 壓縮包的文件名,也可以是壓縮包的路徑。 |
format | 壓縮包種類,“zip”, “tar”, “bztar”,“gztar” |
root_dir | 要壓縮的文件夾路徑(默認當前目錄) |
owner | 用戶,默認當前用戶 |
group | 組,默認當前組 |
shutil對壓縮包的處理是調用ZipFile和TarFile兩個模塊來進行的,後面會介紹這兩個模塊的使用方法。