這篇文章主要講解了Python3如何實現將ipa包中的文件按大小排序,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

給你個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)另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文標題:Python3如何實現將ipa包中的文件按大小排序-創新互聯
轉載注明:http://www.yijiale78.com/article30/idgso.html
成都網站建設公司_創新互聯,為您提供Google、外貿網站建設、營銷型網站建設、服務器托管、ChatGPT、網站排名
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯