Python3將ipa包中的文件按大小排序
給你個(gè)ipa包,解壓前輸出包大小,解壓后把里面的文件按大小排序。
代碼如下:
import os import shutil import zipfile _ipa_zip_path = lambda ipa_path: ipa_path.replace('.ipa', '.zip') _file_size = lambda file_path: os.path.getsize(file_path) / 1024 / 1024 def unzip(zip_path: str) -> str: dir_path = None if zip_path.endswith('.zip'): print(f'{zip_path} file size:{round(_file_size(zip_path),3)}mb') zip_name = os.path.basename(zip_path) dir_name = zip_name.replace('.zip', '') dir_root_path = zip_path.replace(zip_name, '') dir_path = os.path.join(dir_root_path, dir_name) if os.path.exists(dir_path): shutil.rmtree(dir_path) os.mkdir(dir_path) zip_file = zipfile.ZipFile(zip_path) for file_name in zip_file.namelist(): zip_file.extract(file_name, dir_path) zip_file.close() return dir_path def rename_suffix(raw, raw_type, target) -> None: if raw.endswith(raw_type) and os.path.exists(raw): os.rename(raw, target) def walk_files(dir_path) -> list: file_dicts = [] if os.path.exists(dir_path): for root, dirs, files in os.walk(dir_path, topdown=True): for name in files: file_path = os.path.join(root, name) file_dict = { 'file_name': name, 'file_size': round(_file_size(file_path), 8), } file_dicts.append(file_dict) return file_dicts def show_files_size(dir_path=None) -> None: if dir_path: file_dicts_sorted = sorted(walk_files(dir_path), key=lambda e: (e.__getitem__('file_size'), e.__getitem__('file_name')), reverse=True) for file_dict in file_dicts_sorted: print(f'{file_dict["file_name"]}->{file_dict["file_size"]}mb') def ipa_checker(ipa_path: str) -> None: try: ipa_file_size = _file_size(ipa_path) print(f'{ipa_path} file size:{round(ipa_file_size,3)}mb') except FileNotFoundError as error: print(f'File not exists->{ipa_path}') ipa_zip_path = _ipa_zip_path(ipa_path) rename_suffix(ipa_path, '.ipa', ipa_zip_path) try: dir_path = unzip(ipa_zip_path) show_files_size(dir_path) except OSError as error: print(error) if __name__ == '__main__': ipa_path = r'C:\Users\kkk\Desktop\xxx.ipa' ipa_checker(ipa_path)
哦了。
補(bǔ)充知識(shí):Python3將兩個(gè)有序數(shù)組合并為一個(gè)有序數(shù)組
第一種思路,把兩個(gè)數(shù)組合為一個(gè)數(shù)組然后再排序,問題又回歸到冒泡和快排了,沒有用到兩個(gè)數(shù)組的有序性。(不好)
第二種思路,循環(huán)比較兩個(gè)有序數(shù)組頭位元素的大小,并把頭元素放到新數(shù)組中,從老數(shù)組中刪掉,直到其中一個(gè)數(shù)組長度為0。然后再把不為空的老數(shù)組中剩下的部分加到新數(shù)組的結(jié)尾。(好)
第二種思路的排序算法與測(cè)試代碼如下:
def merge_sort(a, b): ret = [] while len(a)>0 and len(b)>0: if a[0] <= b[0]: ret.append(a[0]) a.remove(a[0]) if a[0] >= b[0]: ret.append(b[0]) b.remove(b[0]) if len(a) == 0: ret += b if len(b) == 0: ret += a return ret if __name__ == '__main__': a = [1,3,4,6,7,78,97,190] b = [2,5,6,8,10,12,14,16,18] print(merge_sort(a, b))
反思了一下上面的過程,不應(yīng)該用remove方法,因?yàn)樽屑?xì)想一下remove方法可能比較耗時(shí),不算最簡單。
改進(jìn)一下,改用索引元素比較法替代頭位元素比較法:
def merge_sort(a, b): ret = [] i = j = 0 while len(a) >= i + 1 and len(b) >= j + 1: if a[i] <= b[j]: ret.append(a[i]) i += 1 else: ret.append(b[j]) j += 1 if len(a) > i: ret += a[i:] if len(b) > j: ret += b[j:] return ret if __name__ == '__main__': a = [1,3,4,6,7,78,97,190] b = [2,5,6,8,10,12,14,16,18] print(merge_sort(a, b))
這個(gè)基本就是最簡單的方法了。
以上這篇Python3將ipa包中的文件按大小排序就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python調(diào)用Prometheus監(jiān)控?cái)?shù)據(jù)并計(jì)算
Prometheus是一套開源監(jiān)控系統(tǒng)和告警為一體,由go語言(golang)開發(fā),是監(jiān)控+報(bào)警+時(shí)間序列數(shù)據(jù)庫的組合。本文將介紹Python如何調(diào)用Prometheus實(shí)現(xiàn)數(shù)據(jù)的監(jiān)控與計(jì)算,需要的可以參考一下2021-12-12解決PyCharm的Python.exe已經(jīng)停止工作的問題
今天小編就為大家分享一篇解決PyCharm的Python.exe已經(jīng)停止工作的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11Django框架實(shí)現(xiàn)的簡單分頁功能示例
這篇文章主要介紹了Django框架實(shí)現(xiàn)的簡單分頁功能,在之前一篇留言板之上增加了簡單分頁功能,涉及Paginator模塊的簡單使用技巧,需要的朋友可以參考下2018-12-12Python iter()函數(shù)用法實(shí)例分析
這篇文章主要介紹了Python iter()函數(shù)用法,結(jié)合實(shí)例形式詳細(xì)分析了Python iter()函數(shù)的功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-03-03Python實(shí)現(xiàn)UDP程序通信過程圖解
這篇文章主要介紹了Python實(shí)現(xiàn)UDP程序通信過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python測(cè)試網(wǎng)絡(luò)連通性示例【基于ping】
這篇文章主要介紹了Python測(cè)試網(wǎng)絡(luò)連通性,結(jié)合實(shí)例形式分析了Python通過發(fā)送ping請(qǐng)求測(cè)試網(wǎng)絡(luò)連通性相關(guān)操作技巧,需要的朋友可以參考下2018-08-08