這篇文章主要講解了Python3如何實(shí)現(xiàn)將ipa包中的文件按大小排序,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

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