日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

20180209-shutil模块

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 20180209-shutil模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下面講解shutil模塊的相關操作:

  1.shutil.copyfileobj(fsrc, fdst, length=16*1024) 將fsrc文件內容拷貝到fdst文件中,length是指一次拷貝多少個字節

源碼:
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:breakfdst.write(buf)

  不難發現,copyfileobj方法中length參數是指一次拷貝多少個字符(以字符形式讀取文件,如果以字節的形式讀取,則length值字節數),內部的實現通過一個while死循環來遍歷fsrc文件的length長度字符,并寫入fdst文件中,如果不寫則默認length = 16 * 1024

# 1.將文件內容拷貝到另一個文件中 shutil.copyfileobj(fsrc, fdst, length=16*1024),可以指定一次拷貝多少內容,本例不寫,則默認讀取16 * 1024個字節 ret = shutil.copyfileobj(open('file_01.txt'),open('file_02.txt','w')) print('將文件內容拷貝到另一個文件中:',ret)

  2.shutil.copyfile(src, dst) 拷貝文件,dst目標文件無需存在

#2.拷貝文件 shutil.copyfile(src, dst) dst目標文件無需存在 ret = shutil.copyfile('file_01.txt','file_03.txt') print('拷貝文件:',ret)

  3.shutil.copymode(src,dst)僅拷貝權限,內容、組、用戶均不變,dst目標文件必須存在

# 3.僅拷貝權限,內容、組、用戶均不變 dst目標文件必須存在 ret = shutil.copymode('file_01.txt','file_04.txt') print('僅拷貝權限:',ret)

  4.shutil.copystat(src,dst)僅拷貝狀態的信息,包括mode bits、atime、mtime、flags? dst目標文件必須存在

# 4.僅拷貝文件狀態 包括mode bits、atime、mtime、flags dst目標文件必須存在 ret = shutil.copystat('file_01.txt','file_05.txt') print('僅拷貝文件狀態:',ret)

  5.shutil.copy(src,dst)拷貝文件和權限? dst目標文件無需存在

# 5.shutil.copy(src,dst) 拷貝文件和權限 dst目標文件無需存在 ret = shutil.copy('file_01.txt','file_06.txt') print('拷貝文件和權限:',ret)

  6.shutil.copy2(src,dst)拷貝文件和狀態信息 dst目標文件無需存在

# 6.shutil.copy2(src,dst) 拷貝文件和狀態 dst目標文件無需存在 ret= shutil.copy2('file_01.txt','file_07.txt') print('拷貝文件和狀態:',ret)

  7.shutil.copytree(src, dst, symlinks=False, ignore=None) 遞歸的去拷貝文件夾,ignore參數可以設置忽略文件? dst目標文件不能存在,否則報錯

   ?shutil.ignore_patterns('__init__.py','views.py')

# 7.shutil.copytree(src, dst, symlinks=False, ignore=None) 遞歸的去拷貝文件夾 ret = shutil.copytree('file01','file02',ignore=shutil.ignore_patterns('__init__.py','views.py')) print('遞歸拷貝文件目錄:',ret)

  8.shutil.rmtree(src) 遞歸刪除文件 src源文件必須存在,否則報錯

# 8.shutil.rmtree(path[, ignore_errors[, onerror]]) 遞歸的刪除文件 ret = shutil.rmtree('file02') print('遞歸刪除文件:',ret)

  9.shutil.move(src,dst)遞歸移動文件,src源文件必須存在,否側報錯

# 9.shutil.move(src,dst) 遞歸移動文件,類似mv命令 ret = shutil.move('file01','file02') print('遞歸移動文件:',ret)

重點學習

  項目的目錄結構如下:

?????

  1.壓縮文件
  make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None)
  創建壓縮包并返回文件路徑,例如zip、tar
  base_name 壓縮包的文件名,也可以是壓縮包的路徑,只是文件名時,則保存到當前目錄,否則保存到指定路徑
  format 壓縮包的種類 'zip'、'tar'、'bztar'、'gztar'
  root_dir 要壓縮的文件夾路徑(默認當前目錄)
  logger 用于記錄日志,通常是logging.Logger對象

# 壓縮fil02文件夾,由于shutil_exercise.py所在目錄是shutil下,如果不指定root_dir,怎會將shutil目錄下的所有文件都壓縮到file02.zip包中
# 將/file02下文件壓縮放置到當前目錄下
ret = shutil.make_archive('file02','zip',root_dir='./file02') print('壓縮文件:',ret)

  shutil對壓縮包的處理是調用ZipFile和TarFile兩個模塊進行的,詳細講解:

  zipfile的壓縮/解壓

# 壓縮 z = zipfile.ZipFile('file01.zip','w') z.write('file_01.txt') z.write('file_02.txt') z.write('file02') z.close()# 解壓 # 將file01.zip壓縮包的內容解壓到當前目錄的file01目錄下 z = zipfile.ZipFile('file01.zip','r') z.extractall(path='./file01') z.close()

  注意:zipfile不能壓縮目錄,只能壓縮文件,也就意味著壓縮后的文件中file02只是一個空目錄,目錄下的文件不會被壓縮

?

  tarfile的壓縮/解壓

import tarfile # 壓縮 t = tarfile.open('tmp/file01.tar','w') t.add('file_01.txt',arcname='file_01.bak') t.add('file01') t.close()# 解壓 t = tarfile.TarFile('tmp/file01.tar','r') t.extractall('tmp/tar') t.close()

  注意:tarfile可以壓縮目錄

    

轉載于:https://www.cnblogs.com/it-q/p/8435676.html

總結

以上是生活随笔為你收集整理的20180209-shutil模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。