python之模塊之shutil模塊 shutil -- --High-level file operations 高級的文件操做模塊。 os模塊提供了對目錄或者文件的新建/刪除/查看文件屬性,還提供了對文件以及目錄的路徑操做。好比說:絕對路徑,父目錄…… 可是,os文件的操做還應該包含移動 複製 打包 壓縮 解壓等操做,這些os模塊都沒有提供。 而本章所講的shutil則就是對os中文件操做的補充。--移動 複製 打包 壓縮 解壓, shutil功能: 1 shutil.copyfileobj(fsrc, fdst[, length=16*1024]) #copy文件內容到另外一個文件,能夠copy指定大小的內容 複製代碼 #先來看看其源代碼。 def copyfileobj(fsrc, fdst, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf) #注意! 在其中fsrc,fdst都是文件對象,都須要打開後才能進行復制操做 import shutil f1=open('name','r') f2=open('name_copy','w+') shutil.copyfileobj(f1,f2,length=16*1024) 複製代碼 2 shutil.copyfile(src,dst) #copy文件內容,是否是感受上面的文件複製很麻煩?還須要本身手動用open函數打開文件,在這裏就不須要了,事實上,copyfile調用了copyfileobj 查看源代碼 shutil.copyfile('name','name_copy_2') #一句就能夠實現複製文件內容 3 shutil.copymode(src,dst) #僅copy權限,不更改文件內容,組和用戶。 查看源代碼 複製代碼 #先看兩個文件的權限 [root@slyoyo python_test]# ls -l total 4 -rw-r--r--. 1 root root 79 May 14 05:17 test1 -rwxr-xr-x. 1 root root 0 May 14 19:10 test2 #運行命令 >>> import shutil >>> shutil.copymode('test1','test2') #查看結果 [root@slyoyo python_test]# ls -l total 4 -rw-r--r--. 1 root root 79 May 14 05:17 test1 -rw-r--r--. 1 root root 0 May 14 19:10 test2 #當咱們將目標文件換爲一個不存在的文件時報錯 >>> shutil.copymode('test1','test3') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python/lib/python3.4/shutil.py", line 132, in copymode chmod_func(dst, stat.S_IMODE(st.st_mode)) FileNotFoundError: [Errno 2] No such file or directory: 'test233' 複製代碼 4 shutil.copystat(src,dst) #複製全部的狀態信息,包括權限,組,用戶,時間等 查看源代碼 5 shutil.copy(src,dst) #複製文件的內容以及權限,先copyfile後copymode 複製代碼 def copy(src, dst, *, follow_symlinks=True): """Copy data and mode bits ("cp src dst"). Return the file's destination. The destination may be a directory. If follow_symlinks is false, symlinks won't be followed. This resembles GNU's "cp -P src dst". If source and destination are the same file, a SameFileError will be raised. """ if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) copyfile(src, dst, follow_symlinks=follow_symlinks) copymode(src, dst, follow_symlinks=follow_symlinks) return dst 複製代碼 6 shutil.copy2(src,dst) #複製文件的內容以及文件的全部狀態信息。先copyfile後copystat 查看源代碼 7 shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,ignore_dangling_symlinks=False) #遞歸的複製文件內容及狀態信息 查看源代碼 複製代碼 [root@slyoyo python_test]# tree copytree_test/ copytree_test/ └── test ├── test1 ├── test2 └── hahaha [root@slyoyo test]# ls -l total 0 -rw-r--r--. 1 python python 0 May 14 19:36 hahaha -rw-r--r--. 1 python python 0 May 14 19:36 test1 -rw-r--r--. 1 root root 0 May 14 19:36 test2 >>> shutil.copytree('copytree_test','copytree_copy') 'copytree_copy' [root@slyoyo python_test]# ls -l total 12 drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_copy drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_test -rw-r--r--. 1 python python 79 May 14 05:17 test1 -rw-r--r--. 1 root root 0 May 14 19:10 test2 [root@slyoyo python_test]# tree copytree_copy/ copytree_copy/ └── test ├── hahaha ├── test1 └── test2 複製代碼 8 shutil.rmtree(path, ignore_errors=False, onerror=None) #遞歸地刪除文件 查看源代碼 9 shutil.move(src, dst) #遞歸的移動文件 查看源代碼 10 make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None) #壓縮打包 查看源代碼 base_name: 壓縮打包後的文件名或者路徑名 format: 壓縮或者打包格式 "zip", "tar", "bztar"or "gztar" root_dir : 將哪一個目錄或者文件打包(也就是源文件) 複製代碼 >>> shutil.make_archive('tarball','gztar',root_dir='copytree_test') [root@slyoyo python_test]# ls -l total 12 drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_copy drwxr-xr-x. 3 root root 4096 May 14 19:36 copytree_test -rw-r--r--. 1 root root 0 May 14 21:12 tarball.tar.gz -rw-r--r--. 1 python python 79 May 14 05:17 test1 -rw-r--r--. 1 root root 0 May 14 19:10 test2 複製代碼