import os input directory path inputdirectory = 'T:/Fivem Related/Alpha…
- User
import os
input directory path
input_directory = 'T:/Fivem Related/Alpha Dev/stream_weapons/metas/addon'
loop through all folders in input directory
for folder in os.listdir(input_directory): if os.path.isdir(os.path.join(input_directory, folder)): # do something with folder #print(folder) folderpath = os.path.join(input_directory, folder) for filename in os.listdir(folderpath): if filename.endswith('weapons.meta'): #print('found weapons.meta') file_path = os.path.join(folderpath, filename) with open(file_path, 'r') as file: #print('opened file: %s' % file) lines = file.readlines() # iterate through each line and check if it starts with "" print('hi') for i in range(len(lines)): if lines[i].startswith(''): # print the line below it print(lines[i])
for some reason its not printing the line below
its getting up to the hi print statement
- Assistant
It seems like the problem is with the condition inside the for loop. The condition you have mentioned as "startswith" might not be working as expected due to spaces or any other characters before the actual "" string. You can use the "in" keyword to check for the existence of the string in the line rather than strictly checking the start of the line.
Here is the modified code:
import os # input directory path input_directory = 'T:/Fivem Related/Alpha Dev/stream_weapons/metas/addon' # loop through all folders in input directory for folder in os.listdir(input_directory): if os.path.isdir(os.path.join(input_directory, folder)): # do something with folder #print(folder) folderpath = os.path.join(input_directory, folder) for filename in os.listdir(folderpath): if filename.endswith('weapons.meta'): #print('found weapons.meta') file_path = os.path.join(folderpath, filename) with open(file_path, 'r') as file: #print('opened file: %s' % file) lines = file.readlines() # iterate through each line and check if it starts with "<Item type="CWeaponInfo">" print('hi') for i in range(len(lines)): if '<Item type="CWeaponInfo">' in lines[i]: # print the line below it print(lines[i+1])