Colleagues, you need to find all entries in the file with the format "data \ model \ folder \ folder \ file.extension". There may be several. There is no time to dig up the file structure (the file is not text), so I decided to go by searching for the first match and finding the first occurrence of the file format (tga, dds in both registers), then extracting the slice (file [start: end]). This script returns too many matches from nonexistent places. Where could I make a mistake?
# -*- coding: utf-8 -*- import os import glob folder = r"C:\files\data\model" final_folder = r"C:\files\data\model_patched" files = glob.glob(folder + r'\**\*.fskin', recursive=True) filecount = 0 oc_count = 0 for file in files: with open(file, 'r+b') as f: fcontent = f.read() fcontent = fcontent.decode("utf-16", errors='ignore') cur_pos = 0 extensions = ['.TGA', '.DDS', '.tga', '.dds'] occurences = [] print("file {}:".format(file)) while cur_pos != -1: string_begin = fcontent.find(r"data\model", cur_pos) cur_pos = string_begin # Π½Π°ΡΠΈΠ½Π°Π΅ΠΌ Ρ 0 ΠΈΠ»ΠΈ ΠΊΠΎΠ½ΡΠ° ΠΏΡΠ΅Π΄ΡΠ΄ΡΡΠ΅Π³ΠΎ Π²Ρ
ΠΎΠΆΠ΄Π΅Π½ΠΈΡ string_end = 0 cur_pos_iter = int(cur_pos) # Π§ΡΠΎΠ±Ρ Π½Π΅ ΠΏΠ΅ΡΠ΅Π·Π°ΠΏΠΈΡΠ°ΡΡ cur_pos while string_begin != -1: ch = fcontent[cur_pos_iter:cur_pos_iter+4] # Π±Π΅ΡΡΠΌ 4 ΡΠΈΠΌΠ²ΠΎΠ»Π° Ρ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ if ch not in extensions: # Π΅ΡΠ»ΠΈ Π½Π΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ, ΡΠ²Π΅Π»ΠΈΡΠΈΠ²Π°Π΅ΠΌ ΡΡΡΡΡΠΈΠΊ Π½Π° 1 ΠΈ ΠΈΠ΄ΡΠΌ Π΄Π°Π»ΡΡΠ΅ cur_pos_iter += 1 else: # ΠΡΠ»ΠΈ Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ, ΠΈΡΠ΅ΠΌ Π²Ρ
ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ ΡΠ΅Π»ΠΈΠΊΠΎΠΌ, ΡΡΠ°Π²ΠΈΠΌ ΠΊΡΡΡΠΎΡ Π½Π° ΠΊΠΎΠ½Π΅Ρ Π²Ρ
ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΠΈ Π³Π»ΡΡΠΈΠΌ ΡΠΈΠΊΠ» string_end = cur_pos_iter+4 occurence = fcontent[string_begin:string_end] cur_pos = string_end print("Found {} [{}:{}]".format(occurence, string_begin, string_end)) oc_count += 1 else: filecount += 1 filename_relative = file[file.find('model')+5:] new_filename = final_folder+filename_relative # os.makedirs(os.path.dirname(new_filename), exist_ok=True) # with open(new_filename, 'wb') as fw: # fw.write(bytearray(fcontent, 'utf-16')) # fw.close() f.close() print("Done. Replaced {} occurences in {} files.".format(filecount, oc_count))