亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python 解壓、復制、刪除 文件的實例代碼

 更新時間:2020年02月26日 07:56:42   作者:緣來釋你  
這篇文章主要介紹了python 解壓、復制、刪除 文件的實例代碼,代碼簡單易懂非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

壓縮復制刪除文件基于python語言怎么操作呢,壓縮文件有四種格式:zip、rar、tar、tar.gz,在壓縮過程中也容易出現(xiàn)很多問題,今天小編通過代碼給大家詳解,具體內(nèi)容如下所示:

一、python3解壓文件

1.python 解壓文件代碼示例

如下代碼主要實現(xiàn)zip、rar、tar、tar.gz四種格式的壓縮文件的解壓

def unzip_file(src_file, dst_dir=None, unzipped_files=None, del_flag=True):
 """
 根據(jù)指定的壓縮文件類型遞歸解壓所有指定類型的壓縮文件
 :param src_file: 解壓的源文件路徑,可以為文件夾路徑也可以是文件路徑
 :param dst_dir: 解壓后的文件存儲路徑
 :param unzipped_files: 完成解壓的文件名列表
 :param del_flag: 解壓完成后是否刪除原壓縮文件,默認刪除
 :return: 完成解壓的文件名列表
 """
 # 完成解壓的文件名列表初始為空
 if unzipped_files is None:
  unzipped_files = []
 # 指定的解壓文件類型
 zip_types = ['.zip', '.rar', '.tar', '.gz']

 def exec_decompress(zip_file, dst_dir):
  """
  解壓實現(xiàn)的公共代碼
  :param zip_file: 壓縮文件全路徑
  :param dst_dir: 解壓后文件存儲路徑
  :return:
  """
  file_suffix = os.path.splitext(zip_file)[1].lower()
  try:
   print('Start extracting the file: %s' % zip_file)

   # zip 解壓
   if file_suffix == '.zip':
    # zip解壓 寫法一
    with ZipFile(zip_file, mode='r') as zf:
     zf.extractall(dst_dir)
    # zip解壓 寫法二
    # file_zip = ZipFile(zip_file, mode='r')
    # for file in file_zip.namelist():
    #  file_zip.extract(file, dst_dir)
    # file_zip.close()

   # rar 解壓
   elif file_suffix == '.rar':
    rf = rarfile.RarFile(zip_file)
    rf.extractall(dst_dir)

   # tar、tgz(tar.gz) 解壓
   elif file_suffix in ['.tar', '.gz']:
    tf = tarfile.open(zip_file)
    tf.extractall(dst_dir)
    # 關閉文件釋放內(nèi)存
    tf.close()

   print('Finished extracting the file: %s' % zip_file)
  except Exception as e:
   print(e)
  # 解壓完成加入完成列表
  unzipped_files.append(zip_file)
  # 根據(jù)標識執(zhí)行原壓縮文件刪除
  if del_flag and os.path.exists(zip_file):
   os.remove(zip_file)

 # 如果傳入的文件路徑為文件目錄,則遍歷目錄下所有文件
 if os.path.isdir(src_file):
  # 初始化文件目錄下存在的壓縮文件集合為空
  zip_files = []
  # 如果傳入的目的文件路徑為空,則取解壓的原文件夾路徑
  dst_dir = dst_dir if dst_dir else src_file
  # 遍歷目錄下所有文件
  for file in os.listdir(src_file):
   file_path = os.path.join(src_file, file)
   # 如果是文件夾則繼續(xù)遞歸解壓
   if os.path.isdir(file_path):
    dst_path = os.path.join(dst_dir, file)
    unzip_file(file_path, dst_path, unzipped_files)
   # 如果是指定類型的壓縮文件則加入到壓縮文件列表
   elif os.path.isfile(file_path) and os.path.splitext(file_path)[
    1].lower() in zip_types and file_path not in unzipped_files:
    zip_files.append(file_path)
  # 遍歷壓縮文件列表,執(zhí)行壓縮文件的解壓
  for zip_file in zip_files:
   exec_decompress(zip_file, dst_dir)
  # 如果當前目錄存在壓縮文件則完成所有文件解壓后繼續(xù)遍歷
  if zip_files:
   unzip_file(dst_dir, unzipped_files=unzipped_files)
 # 如果傳入的文件路徑是指定類型的壓縮文件則直接執(zhí)行解壓
 elif os.path.isfile(src_file) and os.path.splitext(src_file)[1].lower() in zip_types:
  dst_dir = dst_dir if dst_dir else os.path.dirname(src_file)
  exec_decompress(src_file, dst_dir)

 return unzipped_files

2.python解壓常見問題解決辦法

2.1 python3 zipfile解壓文件名亂碼解決辦法

直接修改源碼,即 zipfile.py 文件:

第一處:

if flags & 0x800:
 # UTF-8 file names extension
 filename = filename.decode('utf-8')
else:
 # Historical ZIP filename encoding
 # 注釋原代碼
 # filename = filename.decode('cp437')
 # 新加一行代碼
 filename = filename.decode('gbk')

 第二處:

if zinfo.flag_bits & 0x800:
 # UTF-8 filename
 fname_str = fname.decode("utf-8")
else:
 # 注釋原代碼
 # fname_str = fname.decode("cp437")
 # 同樣新加一行代碼
 fname_str = fname.decode('gbk')

2.1 rar 解壓無法找到動態(tài)庫(unrar.dll)解決辦法

報錯示例:

第一步 手動下載動態(tài)庫文件 unrar.dll 存在本地目錄,例如我的本地存儲路徑為:C:\MySoft\assist\unrar.dll

鏈接: https://pan.baidu.com/s/1rqhFND9XmtD1Y8yGLEz9kA 提取碼: u2my

第二步 修改源碼 unrarlib.py 文件

if platform.system() == 'Windows':
 from ctypes.wintypes import HANDLE as WIN_HANDLE
 HANDLE = WIN_HANDLE
 UNRARCALLBACK = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint,
          ctypes.c_long, ctypes.c_long,
          ctypes.c_long)
 # 注釋原代碼
 # lib_path = lib_path or find_library("unrar.dll")
 # 將路徑指向下載的動態(tài)庫文件存儲路徑
 lib_path = r"C:\MySoft\assist\unrar.dll"
 if lib_path:
  unrarlib = ctypes.WinDLL(lib_path)

知識點擴展:python 壓縮文件夾的代碼

def zip_ya(start_dir):
  start_dir = start_dir # 要壓縮的文件夾路徑
  file_news = start_dir + '.zip' # 壓縮后文件夾的名字

  z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED)
  for dir_path, dir_names, file_names in os.walk(start_dir):
   f_path = dir_path.replace(start_dir, '') # 這一句很重要,不replace的話,就從根目錄開始復制
   f_path = f_path and f_path + os.sep or '' # 實現(xiàn)當前文件夾以及包含的所有文件的壓縮
   for filename in file_names:
    z.write(os.path.join(dir_path, filename), f_path + filename)
  z.close()
  return file_news

PS: 若遞歸掃描所有文件夾過程中有文件夾里不存在文件, 該文件夾將被忽略

總結

到此這篇關于python 解壓、復制、刪除 文件的實例代碼的文章就介紹到這了,更多相關python 解壓、復制、刪除 文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Django中cookie的基本使用方法示例

    Django中cookie的基本使用方法示例

    這篇文章主要給大家介紹了關于Django中cookie的基本使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。
    2018-02-02
  • 基于python list對象中嵌套元組使用sort時的排序方法

    基于python list對象中嵌套元組使用sort時的排序方法

    下面小編就為大家分享一篇基于python list對象中嵌套元組使用sort時的排序方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 淺析matlab中imadjust函數(shù)

    淺析matlab中imadjust函數(shù)

    對進行圖像的灰度變換,即調(diào)節(jié)灰度圖像的亮度或彩色圖像的顏色矩陣。這篇文章主要介紹了matlab中imadjust函數(shù),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下
    2020-02-02
  • np.newaxis()函數(shù)的具體使用

    np.newaxis()函數(shù)的具體使用

    本文主要介紹了np.newaxis()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • python使用PyV8執(zhí)行javascript代碼示例分享

    python使用PyV8執(zhí)行javascript代碼示例分享

    這篇文章主要介紹了python使用PyV8執(zhí)行javascript的小示例,大家參考使用吧
    2013-12-12
  • Python基礎之字典的詳細使用教程

    Python基礎之字典的詳細使用教程

    字典作為Python的一個內(nèi)置數(shù)據(jù)結構,和列表一樣都是可變序列的,但是它是無序的,以鍵值對的方式存儲數(shù)據(jù)。本文將詳解一下Python中字典的使用,需要的可以參考一下
    2022-07-07
  • 使用IPython或Spyder將省略號表示的內(nèi)容完整輸出

    使用IPython或Spyder將省略號表示的內(nèi)容完整輸出

    這篇文章主要介紹了使用IPython或Spyder將省略號表示的內(nèi)容完整輸出,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python pptx復制指定頁的ppt教程

    python pptx復制指定頁的ppt教程

    今天小編就為大家分享一篇python pptx復制指定頁的ppt教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python入門基礎之用戶輸入與模塊初認識

    python入門基礎之用戶輸入與模塊初認識

    Python的強大之處在于他有非常豐富和強大的標準庫和第三方庫,幾乎你想實現(xiàn)的任何功能都有相應的Python庫支持。下面通過本文給大家介紹python入門基礎之用戶輸入與模塊初認識,一起看看吧
    2016-11-11
  • python爬蟲 基于requests模塊的get請求實現(xiàn)詳解

    python爬蟲 基于requests模塊的get請求實現(xiàn)詳解

    這篇文章主要介紹了python爬蟲 基于requests模塊的get請求實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08

最新評論