51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import os
|
|
import json
|
|
|
|
def simple_collect_png_filenames(folder_path, output_file="../example/src/main/assets/pic/png_filenames.json"):
|
|
"""
|
|
简化版的PNG文件名收集器(不遍历子文件夹)
|
|
"""
|
|
# 检查文件夹是否存在
|
|
if not os.path.exists(folder_path):
|
|
print(f"错误:文件夹 '{folder_path}' 不存在")
|
|
return []
|
|
|
|
# 获取文件夹中的所有文件
|
|
try:
|
|
all_files = os.listdir(folder_path)
|
|
except PermissionError:
|
|
print(f"错误:没有权限访问文件夹 '{folder_path}'")
|
|
return []
|
|
|
|
# 筛选PNG文件
|
|
png_filenames = []
|
|
for file in all_files:
|
|
file_path = os.path.join(folder_path, file)
|
|
if os.path.isfile(file_path) and file.lower().endswith('.png'):
|
|
png_filenames.append(file)
|
|
|
|
# 按文件名排序
|
|
png_filenames.sort()
|
|
|
|
# 保存到JSON文件
|
|
try:
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
json.dump(png_filenames, f, ensure_ascii=False, indent=2)
|
|
print(f"找到 {len(png_filenames)} 个PNG文件,文件名已保存到 {output_file}")
|
|
except Exception as e:
|
|
print(f"保存文件时出错: {e}")
|
|
|
|
# 显示找到的文件
|
|
if png_filenames:
|
|
print("\n找到的PNG文件:")
|
|
for filename in png_filenames:
|
|
print(f" - {filename}")
|
|
|
|
return png_filenames
|
|
|
|
# 使用方法
|
|
if __name__ == "__main__":
|
|
# 指定要遍历的文件夹
|
|
folder_to_scan = '../example/src/main/assets/pic'
|
|
|
|
simple_collect_png_filenames(folder_to_scan) |