本系列文章來源:<a>https://blog.ansheng.me/article/python-full-stack-way</a>
對文件、文件夾、壓縮包進(jìn)行處理的模塊。
shutil.copyfile( src, dst) 從源src復(fù)制到dst中去。當(dāng)然前提是目標(biāo)地址是具備可寫權(quán)限惨篱。拋出的異常信息為IOException. 如果當(dāng)前的dst已存在的話就會被覆蓋掉
shutil.move( src, dst) 移動文件或重命名
shutil.copymode( src, dst) 只是會復(fù)制其權(quán)限其他的東西是不會被復(fù)制的
shutil.copystat( src, dst) 復(fù)制權(quán)限、最后訪問時間围俘、最后修改時間
shutil.copy( src, dst) 復(fù)制一個文件到一個文件或一個目錄
shutil.copy2( src, dst) 在copy上的基礎(chǔ)上再復(fù)制文件最后訪問時間與修改時間也復(fù)制過來了砸讳,類似于cp –p的東西
shutil.copy2( src, dst) 如果兩個位置的文件系統(tǒng)是一樣的話相當(dāng)于是rename操作,只是改名界牡;如果是不在相同的文件系統(tǒng)的話就是做move操作
shutil.copytree( olddir, newdir, True/Flase)
把olddir拷貝一份newdir簿寂,如果第3個參數(shù)是True,則復(fù)制目錄時將保持文件夾下的符號連接宿亡,如果第3個參數(shù)是False常遂,則將在復(fù)制的目錄下生成物理副本來替代符號連接
shutil.rmtree( src ) 遞歸刪除一個目錄以及目錄內(nèi)的所有內(nèi)容
文件和目錄操作
shutil.copyfileobj(fsrc, fdst[, length])
將文件內(nèi)容拷貝到另一個文件中
>>> import shutil
# 循環(huán)讀取old.txt文件內(nèi)容并寫入到new.txt文件當(dāng)中
>>> shutil.copyfileobj(open('old.txt','r'), open('new.txt', 'w'))
>>> import os
# 查看復(fù)制過去的文件內(nèi)容
>>> os.system("cat old.txt new.txt")
old
old
0
shutil.copyfile(src, dst, *, follow_symlinks=True)
拷貝整個文件,沒有第二個文件就創(chuàng)建挽荠,有就覆蓋
>>> os.system("cat old.txt new.txt")
old
new
0
>>> shutil.copyfile('old.txt', 'new.txt')
# 把第二個文件內(nèi)容給覆蓋了
>>> os.system("cat old.txt new.txt")
old
old
0
shutil.copymode(src, dst, *, follow_symlinks=True)
僅拷貝文件權(quán)限克胳,文件的內(nèi)容平绩、組、用戶均不變
In [9]: ll
total 0
-rw-rw-rw- 1 root 0 Jun 2 15:55 new.txt
-rw-rw-r-- 1 root 0 Jun 2 15:55 old.txt
In [10]: shutil.copymode('old.txt','new.txt')
# 文件權(quán)限都變成644了
In [11]: ll
total 0
-rw-rw-r-- 1 root 0 Jun 2 15:55 new.txt
-rw-rw-r-- 1 root 0 Jun 2 15:55 old.txt
shutil.copystat(src, dst, *, follow_symlinks=True)
拷貝文件狀態(tài)的信息漠另,文件必須存在捏雌,不copy改動時間
>>> os.system("stat old.txt new.txt")
文件:'old.txt'
大小:4 塊:8 IO 塊:4096 普通文件
設(shè)備:801h/2049d Inode:1835014 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
文件:'new.txt'
大邪蚀辍:4 塊:8 IO 塊:4096 普通文件
設(shè)備:801h/2049d Inode:1835024 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
0
>>> shutil.copystat('old.txt', 'new.txt')
>>> os.system("stat old.txt new.txt")
文件:'old.txt'
大行允:4 塊:8 IO 塊:4096 普通文件
設(shè)備:801h/2049d Inode:1835014 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
文件:'new.txt'
大小:4 塊:8 IO 塊:4096 普通文件
設(shè)備:801h/2049d Inode:1835024 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
0
shutil.copy(src, dst, *, follow_symlinks=True)
拷貝文件和狀態(tài)信息满败,同樣不copy改動時間
>>> os.system("stat old.txt new.txt")
文件:'old.txt'
大芯阶唷:4 塊:8 IO 塊:4096 普通文件
設(shè)備:801h/2049d Inode:1835014 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
文件:'new.txt'
大小:0 塊:0 IO 塊:4096 普通空文件
設(shè)備:801h/2049d Inode:1835023 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
0
>>> shutil.copy2('old.txt', 'new.txt')
>>> os.system("stat old.txt new.txt")
文件:'old.txt'
大泻肌:4 塊:8 IO 塊:4096 普通文件
設(shè)備:801h/2049d Inode:1835014 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
文件:'new.txt'
大小:4 塊:8 IO 塊:4096 普通文件
設(shè)備:801h/2049d Inode:1835023 硬鏈接:1
權(quán)限:(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
創(chuàng)建時間:-
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
可選參數(shù)如下:
base_name 壓縮包的文件名熬苍,也可以是壓縮包的路徑。
format 壓縮包種類袁翁,“zip”, “tar”, “bztar”柴底,“gztar”
root_dir 要壓縮的文件夾路徑(默認(rèn)當(dāng)前目錄)
owner 用戶,默認(rèn)當(dāng)前用戶
group 組粱胜,默認(rèn)當(dāng)前組
shutil對壓縮包的處理是調(diào)用ZipFile和TarFile兩個模塊來進(jìn)行的.