0

So basically, I have a super long output, and I want to be able to save this output to a brand new text file, without changing my old file. This is my code thus far, I basically just need to know what to do with my out_file variable.

Thanks for all the future help.

txt_list = [] with open("X:\- Photogrammetry\Python to Correct DXF Colors\InvolvedPontiac.dxf", "r") as in_file, open("X:\- Photogrammetry\Python to Correct DXF Colors\InvolvedPontiacfixed.txt", "w+") as new_file: for line in in_file: line = line.rstrip() txt_list.append(line) Entity = 0 i = 0 while i < len(txt_list): if txt_list[i] == "ENTITIES": Entity = 1 if txt_list[i] == " 62" and Entity == 1: txt_list[i+1] = " 256" i += 1 Layer = 0 j = 0 while j < len(txt_list): if txt_list[j] == "LAYER" and txt_list[j+2] != " 7": Userinput = input("What color would you like for the layer " +txt_list[j+2] + "? Type 0 for black, 1 for red, 3 for green, 4 for light blue, 5 for blue, 6 for magenta, 7 for white, 8 for dark grey, 9 for medium gray, or 30 for orange.") txt_list[j+6] = " " + Userinput print ("The " + txt_list[j+2] + " layer now has a color code of " + Userinput) j += 1 for item in txt_list: new_file.write(item) print ('\n'.join(txt_list)) 

1 Answer 1

1

I'm not sure exactly what's going on in your code.

But to write a variable to a file, you can use

with open('output.txt', 'w+') as new_file: new_file.write(variable) 

Note that the 'w+' will create the file if it doesn't exist, and will overwrite it if it does.

And if it's all the items in txt_list you want to write to that file, I don't think I'd join them first. Just use a for loop:

with open('output.txt', 'w+') as new_file: for item in txt_list: new_file.write(item) 

This would print every item in that list on a new line in the file.

txt_list = [] with open("X:\- Photogrammetry\Python to Correct DXF Colors\InvolvedPontiac.dxf", "r") as in_file: for line in in_file: line = line.rstrip() txt_list.append(line) Entity = 0 i = 0 while i < len(txt_list): if txt_list[i] == "ENTITIES": Entity = 1 if txt_list[i] == " 62" and Entity == 1: txt_list[i+1] = " 256" i += 1 Layer = 0 j = 0 while j < len(txt_list): if txt_list[j] == "LAYER" and txt_list[j+2] != " 7": Userinput = input("What color would you like for the layer " +txt_list[j+2] + "? Type 0 for black, 1 for red, 3 for green, 4 for light blue, 5 for blue, 6 for magenta, 7 for white, 8 for dark grey, 9 for medium gray, or 30 for orange.") txt_list[j+6] = " " + Userinput print ("The " + txt_list[j+2] + " layer now has a color code of " + Userinput) j += 1 with open('output.txt', 'w+') as new_file: for item in txt_list: new_file.write(item) print ('\n'.join(txt_list) 
Sign up to request clarification or add additional context in comments.

11 Comments

Sorry about the crazy code, it is doing stuff with the text file that is opened. I added your statement at the end but got this error out_file.write(item) ValueError: I/O operation on closed file.
That error says you tried to write using the variable name of your first file. I think that might be on me, as I used a similar variable name. Let me edit.
Even with that, I still get the same error. Could it have to do with my indentation? Is the file closing before I write to it?
You're not opening the new file first. :D Edited again.
Sorry I'm so useless, but isn't it being opened at the top of my code, where I also open the in_file?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.