Help with importing from multiple files and printing lines in designated spot to spit out one file.

Joined
Jan 16, 2023
Messages
1
Reaction score
0
I have a python script that will take a text file and insert each line of text into an appropriate column. What I would like to do is import lines of text from multiple files and have it write the string in an appropriate area. Here is the code snippet.

input_file = input("Path To The Class File: ")
output_file = input("Output File Path: ")

text_file = open(input_file, "r")
output_file = open(f"{output_file}/Output.txt", "a")
lines = text_file.readlines()
final_lines = []
for x in lines:
z = x.replace("\n", "")
final_lines.append(z)
b = ""

for in final_lines:
output_file.write(' {\n')
output_file.write(f' "dna_Tier": "yellow",\n')
output_file.write(f' "dna_Helm": "{i}",\n')
output_file.write(f' "dna_Shirt": "",\n')
output_file.write(f' "dna_Vest": "",\n')
output_file.write(f' "dna_Pants": "",\n')
output_file.write(f' "dna_Shoes": "", \n')
output_file.write(f' "dna_Backpack": "",\n')
output_file.write(f' "dna_Gloves": "",\n')
output_file.write(f' "dna_Belt": "",\n')
output_file.write(f' "dna_Facewear": "",\n')
output_file.write(f' "dna_Eyewear": "",\n')
output_file.write(f' "dna_Armband": "",\n')
output_file.write(f' "dna_NVG": ""\n')
output_file.write(' },\n')


output_file.close()
text_file.close()

It is for creating a json file format, and what I would like to do is import from multiple files. Each file would correspond with a type of clothing. I would like the script to write from corresponding file into a corresponding column.

TIA
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
Here is an updated version of the code that allows for input of multiple files and writing the lines from each file into corresponding columns in the output file:

Python:
input_files = []
output_file = input("Output File Path: ")

while True:
file_path = input("Enter the path of a file or type 'q' to stop: ")
if file_path == 'q':
break
input_files.append(file_path)

output_file = open(f"{output_file}/Output.txt", "a")

final_lines = []
for input_file in input_files:
text_file = open(input_file, "r")
lines = text_file.readlines()
for x in lines:
z = x.replace("\n", "")
final_lines.append(z)
text_file.close()

b = ""

for i in final_lines:
output_file.write(' {\n')
if input_files.index(input_file) == 0:
output_file.write(f' "dna_Helm": "{i}",\n')
elif input_files.index(input_file) == 1:
output_file.write(f' "dna_Shirt": "{i}",\n')
elif input_files.index(input_file) == 2:
output_file.write(f' "dna_Vest": "{i}",\n')
# add more elif statements for additional input files as needed
else:
output_file.write(f' "dna_Tier": "yellow",\n')
output_file.write(f' "dna_Pants": "",\n')
output_file.write(f' "dna_Shoes": "", \n')
output_file.write(f' "dna_Backpack": "",\n')
output_file.write(f' "dna_Gloves": "",\n')
output_file.write(f' "dna_Belt": "",\n')
output_file.write(f' "dna_Facewear": "",\n')
output_file.write(f' "dna_Eyewear": "",\n')
output_file.write(f' "dna_Armband": "",\n')
output_file.write(f' "dna_NVG": ""\n')
output_file.write(' },\n')

output_file.close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top