常用模組(shutil copy、壓縮、解壓)
阿新 • • 發佈:2019-01-05
作用與功能
主要用於檔案的copy,壓縮,解壓
匯入shuitl模組:
import shutil
copy方法
1 1、shutil.copyfileobj() 開啟file1,並copy寫入file2: 2 with open("筆記1",'r',encoding='utf-8') as f1,open('筆記2','w',encoding='utf-8') as f2: 3 shutil.copyfileobj(f1,f2) 4 5 6 7 #輸入檔名就能直接拷貝(呼叫copyfileobj方法)8 shutil.copyfile("筆記1","筆記3") 9 10 11 12 #拷貝許可權,內容,組,使用者均不變:(win看不出來,linux下可以嘗試) 13 shutil.copymode("筆記1","筆記3") 14 15 16 17 #拷貝狀態的資訊(只拷貝許可權,不建立檔案),包括:mode bits,atime,mtime,flags 18 shutil.copystat("筆記1","筆記3") 19 20 21 22 #拷貝檔案和許可權: 23 shutil.copy("筆記1","筆記3") 24 25 26 27 #拷貝檔案和狀態資訊:(檔案和許可權)28 shutil.copy2("筆記1","筆記3") 29 30 31 32 33 #遞迴的去copy檔案:(copy目錄) 34 shutil.copytree(r"D:\a",r"D:\a1") 35 例如:用python指令碼實現程式碼釋出指定線上伺服器,例如svn和git在釋出的時候,有些檔案是不需要進行拷貝的,因襲就需要進行過濾 36 方法如下: 37 shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) 38 39 40 41 #遞迴的刪除目錄:(有連結佔中目錄,刪除報錯)42 shutil.rmtree(r"D:\a1") 43 44 45 46 #移動檔案: 47 shutil.move(r"D:\a",r"D:\a1")
壓縮和解壓縮方法
1 1、全目壓縮: 2 #建立壓縮包,並返回檔案路徑:例如:zip tar 3 #建立壓縮包並返回檔案路徑,例如:zip、tar 4 #格式:shutil.make_archive(base_name,format(zip),root_dir,owner,group,logger) 5 # base_name: 壓縮包的檔名,也可以是壓縮包的路徑。只是檔名時,則儲存至當前目錄,否則儲存至指定路徑, 6 # 如:www =>儲存至當前路徑 7 # 如:/Users/wupeiqi/www =>儲存至/Users/wupeiqi/ 8 # format: 壓縮包種類,“zip”, “tar”, “bztar”,“gztar” 9 # root_dir: 要壓縮的資料夾路徑(預設當前目錄) 10 # owner: 使用者,預設當前使用者 11 # group: 組,預設當前組 12 # logger: 用於記錄日誌,通常是logging.Logger物件 13 #將 /Users/wupeiqi/Downloads/test 下的檔案打包放置當前程式目錄 14 15 import shutil 16 ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test') 17 18 #將 /Users/wupeiqi/Downloads/test 下的檔案打包放置 /Users/wupeiqi/目錄 19 import shutil 20 ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test') 21 22 #將D:\軟體\pychar\data\s13\Atm目錄下的檔案打包放置在D:\軟體\pychar\data\s13\Atm_name_tar檔案下 23 shutil.make_archive(r"D:\軟體\pychar\data\s13\Atm_name_tar","tar","D:\軟體\pychar\data\s13\Atm") 24 25 ========================================================================== 26 三、解壓方法和指定檔案的壓縮和解壓: 27 28 2、Zip單個檔案壓縮與解壓:(打包在壓縮) 29 (壓縮包也可以當做一個檔案,想要加入壓縮檔案的話可以直接寫進壓縮包裡) 30 31 2.1:#寫入指定壓縮檔案(w) 32 z = zipfile.ZipFile(r'D:\軟體\pychar\data\test\node.zip', 'w') 33 z.write('筆記1') 34 z.write('筆記3') 35 z.close() 36 37 2.2 #追加指定壓縮(a) 38 z = zipfile.ZipFile(r'D:\軟體\pychar\data\test\node.zip', 'a') 39 z.write('test.py') 40 z.write('md_sys_test.py') 41 z.close() 42 43 44 2.3:z.extractall() 解壓所有檔案:(所有檔案) 45 os.chdir(r"D:\軟體\pychar\data\test") 46 z = zipfile.ZipFile("node.zip",'r') 47 z.extractall() 48 z.close() 49 50 51 2.4:z.extract('test.py') 解壓指定檔案: 52 只需要傳輸字串格式的檔名即可 53 os.chdir(r"D:\軟體\pychar\data\test") 54 z = zipfile.ZipFile("node.zip",'r') 55 for item in z.namelist(): 56 if item == 'test.py': 57 z.extract('test.py') 58 z.close() 59 60 61 62 63 64 ============================================================================================= 65 3、tar單個檔案壓縮與解壓:(tar只打包不壓縮) 66 3.1、寫入指定壓縮檔案(w) 67 import tarfile 68 69 tar = tarfile.open(r'D:\軟體\pychar\data\test\your.tar','w') 70 tar.add(r'D:\軟體\pychar\data\test\test.py', arcname='bbs2.log') 71 tar.add(r'D:\軟體\pychar\data\test\md_sys_test.py', arcname='cmdb.log') 72 tar.close() 73 74 3.2、新增指定壓縮檔案(a) 75 tar = tarfile.open(r'D:\軟體\pychar\data\test\your.tar','a') 76 tar.add(r'D:\軟體\pychar\data\test\筆記1', arcname='node1.txt') 77 tar.add(r'D:\軟體\pychar\data\test\筆記3', arcname='node3.txt') 78 tar.close() 79 80 81 3.3、解壓所有檔案 82 os.chdir(r"D:\軟體\pychar\data\test") 83 tar = tarfile.open('your.tar','r') 84 tar.extractall() # 可設定解壓地址 85 tar.close() 86 87 3.4、解壓指定檔案 88 tar.getmembers():遍幣所有壓縮包內的檔案物件(非檔案字串) 89 tar.getmember("node1.txt"):指定壓縮包內的某個檔案 90 os.chdir(r"D:\軟體\pychar\data\test") 91 tar = tarfile.open('your.tar','r') 92 for item in tar.getmembers(): 93 job = tar.getmember("node1.txt") 94 if item == job: 95 tar.extract(job) 96 tar.close()