Поиск имени файла внутри папки с файлами jarPython

Программы на Python
Ответить
Anonymous
 Поиск имени файла внутри папки с файлами jar

Сообщение Anonymous »

У меня возникла проблема при моддинге Minecraft. Что-то добавило тег c:coal к minecraft:charcoal, что вызвало у меня проблемы. Поэтому вместо того, чтобы открывать каждый из моих 200 файлов .jar в папке модов, я написал быстрый скрипт на Python 3.12.4 для поиска файла Coal.json внутри файлов .jar.
Проблема в том, что скрипт не работает. Точнее, скрипт запускается и ничего не находит. Я даже тестировал поиск других распространенных имен файлов, например ores.json, но все равно сказал, что ничего не нашел.
Может кто-нибудь сказать мне, что не так со сценарием, или есть ли Более простой способ решить мою первоначальную проблему с поиском файла Coal.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}")


Подробнее здесь: https://stackoverflow.com/questions/792 ... -jar-files
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»