-
Notifications
You must be signed in to change notification settings - Fork 9
/
program_changeFile.py
35 lines (32 loc) · 1.54 KB
/
program_changeFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#Modify the cfg text for each part.
import os
folder_path = r'C:\Games\KSP\1.12.5 Stock\GameData\PhotonCorp\Parts\SpaceShuttleSRB'
field_name_1 = 'title'
field_name_2 = 'description'
field_name_3 = 'name'
for dirpath, dirnames, filenames in os.walk(folder_path):
folder_name = os.path.basename(dirpath)
for filename in filenames:
if filename.endswith('.cfg'):
with open(os.path.join(dirpath, filename), 'r') as file:
content = file.read()
lines = content.split('\n')
field_value_3 = ''
found_field_1 = False
found_field_2 = False
found_field_3 = False
for i, line in enumerate(lines):
if field_name_1 in line and not found_field_1:
lines[i] = f'\ttitle = #LOC_{field_value_3}_title'
found_field_1 = True
if field_name_2 in line and not found_field_2:
lines[i] = f'\tdescription = #LOC_{field_value_3}_description'
found_field_2 = True
if field_name_3 in line and not found_field_3:
field_value_3 = line.split('=')[1].strip()
found_field_3 = True
if found_field_1 and found_field_2 and found_field_3:
break
content = '\n'.join(lines)
with open(os.path.join(dirpath, filename), 'w') as file:
file.write(content)