python 解壓、復(fù)制、刪除 文件的實(shí)例代碼
壓縮復(fù)制刪除文件基于python語(yǔ)言怎么操作呢,壓縮文件有四種格式:zip、rar、tar、tar.gz,在壓縮過(guò)程中也容易出現(xiàn)很多問(wèn)題,今天小編通過(guò)代碼給大家詳解,具體內(nèi)容如下所示:
一、python3解壓文件1.python 解壓文件代碼示例如下代碼主要實(shí)現(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: 解壓后的文件存儲(chǔ)路徑 :param unzipped_files: 完成解壓的文件名列表 :param del_flag: 解壓完成后是否刪除原壓縮文件,默認(rèn)刪除 :return: 完成解壓的文件名列表 ''' # 完成解壓的文件名列表初始為空 if unzipped_files is None: unzipped_files = [] # 指定的解壓文件類型 zip_types = [’.zip’, ’.rar’, ’.tar’, ’.gz’] def exec_decompress(zip_file, dst_dir): ''' 解壓實(shí)現(xiàn)的公共代碼 :param zip_file: 壓縮文件全路徑 :param dst_dir: 解壓后文件存儲(chǔ)路徑 :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解壓 寫(xiě)法一 with ZipFile(zip_file, mode=’r’) as zf: zf.extractall(dst_dir) # zip解壓 寫(xiě)法二 # 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) # 關(guān)閉文件釋放內(nèi)存 tf.close() print(’Finished extracting the file: %s’ % zip_file) except Exception as e: print(e) # 解壓完成加入完成列表 unzipped_files.append(zip_file) # 根據(jù)標(biāo)識(shí)執(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) # 如果當(dāng)前目錄存在壓縮文件則完成所有文件解壓后繼續(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_files2.python解壓常見(jiàn)問(wèn)題解決辦法
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 解壓無(wú)法找到動(dòng)態(tài)庫(kù)(unrar.dll)解決辦法
報(bào)錯(cuò)示例:
第一步 手動(dòng)下載動(dòng)態(tài)庫(kù)文件 unrar.dll 存在本地目錄,例如我的本地存儲(chǔ)路徑為: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') # 將路徑指向下載的動(dòng)態(tài)庫(kù)文件存儲(chǔ)路徑 lib_path = r'C:MySoftassistunrar.dll' if lib_path: unrarlib = ctypes.WinDLL(lib_path)
知識(shí)點(diǎn)擴(kuò)展: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的話,就從根目錄開(kāi)始復(fù)制 f_path = f_path and f_path + os.sep or ’’ # 實(shí)現(xiàn)當(dāng)前文件夾以及包含的所有文件的壓縮 for filename in file_names: z.write(os.path.join(dir_path, filename), f_path + filename) z.close() return file_news
PS: 若遞歸掃描所有文件夾過(guò)程中有文件夾里不存在文件, 該文件夾將被忽略
總結(jié)
到此這篇關(guān)于python 解壓、復(fù)制、刪除 文件的實(shí)例代碼的文章就介紹到這了,更多相關(guān)python 解壓、復(fù)制、刪除 文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. HTML <!DOCTYPE> 標(biāo)簽2. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移3. HTML5 Canvas繪制圖形從入門(mén)到精通4. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案5. Django中如何使用Channels功能6. Vue ElementUI實(shí)現(xiàn):限制輸入框只能輸入正整數(shù)的問(wèn)題7. 基于Python下載網(wǎng)絡(luò)圖片方法匯總代碼實(shí)例8. .NET Core中使用gRPC的方法9. php對(duì)gb編碼動(dòng)態(tài)轉(zhuǎn)utf-8編碼的幾種方法評(píng)測(cè)10. 基于JavaScript實(shí)現(xiàn)大文件上傳后端代碼實(shí)例
