python - zipfile模組
阿新 • • 發佈:2018-12-25
import zipfile # f=zipfile.ZipFile(filename, mode="r", compression=ZIP_STORED, allowZip64=False) # filename是一個檔案的路徑 # mode a :追加模式 # mode r :只讀模式 # mode w :寫入模式 # allowZip64:是個bool型變數,當設定為True的時候就是說可以用來建立大小大於2G的zip檔案,預設值是True; # 可判斷是否是壓縮檔案: # zipfile.is_zipfile(filename) # 將多個檔案壓縮成一個檔案.# with zipfile.ZipFile('test2.zip', mode='w') as zipf: # zipf.write('a2.txt') # zipf.write('a3.txt') # zipf.write('a4.txt') # zipf.write('a.txt') # 將檔案解壓到 abc檔案內(當前檔案下的abc檔案) # with zipfile.ZipFile('test.zip') as zipf: # zipf.extractall('abc') # 獲取壓縮包內的檔名: # with zipfile.ZipFile('test.zip','r') as zipf:# for i in zipf.namelist(): # print(i) # 獲取壓縮包內的檔案物件,可檢視檔名,大小,mode # with zipfile.ZipFile('test.zip','r') as zipf: # for i in zipf.infolist(): # print(i) # <ZipInfo filename='a2.txt' filemode='-rw-rw-rw-' file_size=43> # 將zip檔案的目錄結構列印到stdout上,包括每個檔案的path,修改時間和大小 # with zipfile.ZipFile('test.zip','r') as zipf:# zipf.printdir() # 開啟壓縮檔案中的一個檔案 # with zipfile.ZipFile('test.zip') as zipf: # info = zipf.open("a.txt",'r') # print(info.read())