av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:2日期:2022-08-06 13:02:39

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

一、python3解壓文件1.python 解壓文件代碼示例

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

def unzip_file(src_file, dst_dir=None, unzipped_files=None, del_flag=True): ''' 根據指定的壓縮文件類型遞歸解壓所有指定類型的壓縮文件 :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): ''' 解壓實現的公共代碼 :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) # 關閉文件釋放內存 tf.close() print(’Finished extracting the file: %s’ % zip_file) except Exception as e: print(e) # 解壓完成加入完成列表 unzipped_files.append(zip_file) # 根據標識執行原壓縮文件刪除 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) # 如果是文件夾則繼續遞歸解壓 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) # 遍歷壓縮文件列表,執行壓縮文件的解壓 for zip_file in zip_files: exec_decompress(zip_file, dst_dir) # 如果當前目錄存在壓縮文件則完成所有文件解壓后繼續遍歷 if zip_files: unzip_file(dst_dir, unzipped_files=unzipped_files) # 如果傳入的文件路徑是指定類型的壓縮文件則直接執行解壓 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_files2.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 解壓無法找到動態庫(unrar.dll)解決辦法

報錯示例:

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

第一步 手動下載動態庫文件 unrar.dll 存在本地目錄,例如我的本地存儲路徑為:C:MySoftassistunrar.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') # 將路徑指向下載的動態庫文件存儲路徑 lib_path = r'C:MySoftassistunrar.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 ’’ # 實現當前文件夾以及包含的所有文件的壓縮 for filename in file_names: z.write(os.path.join(dir_path, filename), f_path + filename) z.close() return file_news

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

總結

到此這篇關于python 解壓、復制、刪除 文件的實例代碼的文章就介紹到這了,更多相關python 解壓、復制、刪除 文件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美日韩高清在线观看 | 国产一区二区久久 | 国产免费一区二区 | 国产综合在线视频 | 蜜桃视频一区二区三区 | 在线观看日本网站 | 91资源在线 | 国产精品毛片 | 最新国产在线 | 国产精品久久国产精品 | 国产女人与拘做受视频 | 免费看淫片 | 国产一级在线观看 | 欧美黄色大片在线观看 | 久久精品一 | 91中文视频 | 羞羞视频网站在线观看 | 久久综合一区 | 一区二区三区国产好 | 国产一区 | 国产精品久久久亚洲 | 91视频免费视频 | 成人1区 | 国产在线激情视频 | 神马九九 | 中文字幕国产一区 | 久久成人一区 | 国产男女精品 | 国产成人精品免高潮在线观看 | 午夜免费观看体验区 | 国产精品久久一区二区三区 | 国产精品精品久久久久久 | 欧美精品一区三区 | 青青草国产在线观看 | 国产精品一区一区三区 | 日韩高清在线观看 | 久久精品视频在线观看 | 欧美综合国产精品久久丁香 | 久久四虎| 午夜av影院 | 亚洲日本国产 |