我在修改《我的世界》时遇到了问题。有些东西在我的世界:木炭中添加了 c:coal 标签,给我带来了问题。因此,我没有分别打开 mods 文件夹中的 200 个 .jar 文件,而是使用 Python 3.12.4 编写了一个快速脚本来搜索 .jar 文件中的煤炭.json 文件。
我遇到的问题是该脚本似乎不起作用。准确地说,脚本运行并没有发现任何内容。我什至测试了查找其他常见文件名,例如 ores.json,但仍然告诉我它什么也没找到。
有人可以告诉我脚本出了什么问题吗?或者是否有更简单的方法来解决我最初的定位煤炭.json 文件的问题?
这是我的脚本:
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"
target_path = "data\\c\\tags\\item\\"
target_file = "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:
# Check if the target path and file exists in the .jar archive
for entry in jar.namelist():
# Check if the entry matches the target path and file
if entry == f"{target_path}{target_file}":
print(f"Found '{target_file}' in: {jar_path}")
found_jar_files.append(jar_path)
break # Stop checking further entries in this .jar
except zipfile.BadZipFile:
print(f"Error: {jar_path} is not a valid .jar file.")
# Print results
if found_jar_files:
print(f"\nThe following .jar files contain '{target_file}':")
for jar_file in found_jar_files:
print(jar_file)
else:
print(f"No '{target_file}' found in any .jar files in the folder: {mods_folder}")
刚刚编辑,所以 target_path = "data/c/tags/item/" # 确保使用正斜杠
现在,它将与 jar.namelist() 生成的内容匹配