返回
Python一键查找iOS项目中未使用的图片、音频、视频资源
IOS
2023-09-12 08:23:53
# 导入必要的库
import os
import re
# 定义项目根目录
project_root = "/path/to/project"
# 定义要查找的资源类型
resource_types = ["png", "jpg", "jpeg", "gif", "mp3", "m4a", "mp4", "mov"]
# 遍历项目根目录下的所有文件
for root, dirs, files in os.walk(project_root):
# 遍历所有文件
for file in files:
# 获取文件扩展名
file_extension = os.path.splitext(file)[1]
# 如果文件扩展名在要查找的资源类型中
if file_extension in resource_types:
# 获取文件的绝对路径
file_path = os.path.join(root, file)
# 使用正则表达式查找文件是否被引用
is_used = False
with open(file_path, "r") as f:
for line in f:
if re.search(file, line):
is_used = True
break
# 如果文件没有被引用,则打印文件路径
if not is_used:
print(file_path)
前言
在 iOS 项目开发的过程中,如果版本迭代开发的时间比较长,那么在很多版本开发以后或者说有多人开发参与以后,工程中难免有一些垃圾资源,未被使用却占据着 api 包的大小!这里我通过 Python 脚本来查找项目中未被使用的图片、音频、视频资源,并将其删除。
实现步骤
- 导入必要的库
import os
import re
- 定义项目根目录
project_root = "/path/to/project"
- 定义要查找的资源类型
resource_types = ["png", "jpg", "jpeg", "gif", "mp3", "m4a", "mp4", "mov"]
- 遍历项目根目录下的所有文件
for root, dirs, files in os.walk(project_root):
- 遍历所有文件
for file in files:
- 获取文件扩展名
file_extension = os.path.splitext(file)[1]
- 如果文件扩展名在要查找的资源类型中
if file_extension in resource_types:
- 获取文件的绝对路径
file_path = os.path.join(root, file)
- 使用正则表达式查找文件是否被引用
is_used = False
with open(file_path, "r") as f:
for line in f:
if re.search(file, line):
is_used = True
break
- 如果文件没有被引用,则打印文件路径
if not is_used:
print(file_path)
结语
通过使用 Python 脚本,我们可以轻松地查找和删除项目中未使用的资源,从而减小项目大小,提高性能,并使代码更整洁。