求助:Python脚本无法在Minecraft模组Jar包中定位指定文件的问题排查
求助:Python脚本无法在Minecraft模组Jar包中定位指定文件的问题排查
我来帮你排查这个问题!首先你的核心需求是找到哪个模组Jar包里包含了data/c/tags/item/coal.json这个文件,你的脚本逻辑方向是对的,但有几个关键细节没处理好,导致找不到目标文件。
一、脚本的核心问题:路径分隔符不匹配
Zip/Jar文件内部的目录分隔符永远是正斜杠/,不管你用的是Windows还是Linux系统。但你的脚本里用了Windows的反斜杠\\来构造target_path,导致拼接出来的目标路径是data\\c\\tags\\item\\coal.json,而Jar包内实际的路径是data/c/tags/item/coal.json——这两个字符串完全不相等,所以你的匹配条件entry == f"{target_path}{target_file}"永远不会触发。
二、优化后的修复脚本
我给你调整了脚本,解决了路径问题,同时还优化了检查逻辑(不用遍历所有条目,直接用zipfile的内置方法判断,更高效):
import os import zipfile # Define the path to the mods folder and the target file path mods_folder = r"C:\Users\***\AppData\Roaming\PrismLauncher\instances\1.21\.minecraft\mods" # 统一用正斜杠构造Zip内部的目标路径,适配Jar包的存储格式 target_full_path = "data/c/tags/item/coal.json" # List to store the names of .jar files containing the target file found_jar_files = [] # Iterate over all .jar files in the mods folder for root, dirs, files in os.walk(mods_folder): for file in files: if file.endswith(".jar"): jar_path = os.path.join(root, file) try: # Open the .jar file as a zip archive with zipfile.ZipFile(jar_path, 'r') as jar: # 直接检查目标路径是否存在于Jar包中,无需遍历所有条目 if target_full_path in jar.namelist(): print(f"Found '{target_full_path}' in: {jar_path}") found_jar_files.append(jar_path) except zipfile.BadZipFile: print(f"Error: {jar_path} is not a valid .jar file.") except Exception as e: print(f"Unexpected error processing {jar_path}: {str(e)}") # Print results if found_jar_files: print(f"\nThe following .jar files contain the target file:") for jar_file in found_jar_files: print(jar_file) else: print(f"No '{target_full_path}' found in any .jar files in the folder: {mods_folder}")
三、不用写脚本的更简单方法
如果你不想折腾Python脚本,用现成工具更快:
- Windows用户:
- 用7-Zip批量搜索:安装7-Zip后,打开命令提示符,运行:
for /r "C:\Users\***\AppData\Roaming\PrismLauncher\instances\1.21\.minecraft\mods" %f in (*.jar) do "C:\Program Files\7-Zip\7z.exe" l "%f" | findstr "data/c/tags/item/coal.json" && echo Found in: %f - 用Everything工具:在搜索框输入
*.jar content:data/c/tags/item/coal.json(需要开启Everything的内容搜索功能,并安装7-Zip插件)。
- 用7-Zip批量搜索:安装7-Zip后,打开命令提示符,运行:
- 通用方法:用Total Commander的「搜索文件」功能,勾选「搜索压缩包内的文件」,然后输入目标文件路径即可。
四、额外提醒
如果还是找不到,可能要考虑:
- 目标文件的路径是不是有变体?比如有些模组可能把标签放在自己的命名空间下,而不是
c命名空间?不过你提到的是c:coal标签,所以命名空间应该是c,这个路径是对的。 - 有没有可能是模组的Jar包损坏?脚本里已经处理了
BadZipFile异常,可以留意错误提示。
备注:内容来源于stack exchange,提问作者Fernando Fuentes Martins
相关产品推荐
相关产品推荐

