Python Forum
write each line of a text file into separate text files and save with different names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write each line of a text file into separate text files and save with different names
#1
Hi guys, I have a text file which contains the following value:
Output:
('Yellow_Hat_Person', [293, 997, [...]], [328, 1031, [...]]) ('Yellow_Hat_Person', [292, 998, [...]], [326, 1032, [...]]) ('Yellow_Hat_Person', [290, 997, [...]], [324, 1030, [...]]) ('Yellow_Hat_Person', [288, 997, [...]], [321, 1028, [...]]) ('Yellow_Hat_Person', [286, 995, [...]], [319, 1026, [...]])
I want to write each line into separate text files and save them with a different names. eg. line 1 should be passed to a text file and it should be saved as 1.txt, line to should be passed to a different text file and it should be saves ad 2.txt, line 3 passed to a different text file and saved as 3.txt and so on. Any suggestions would be helpful.
Reply
#2
what have you tried? For start - take a look at our tutorial on working with files
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
nvm. I solved it by enumerating over the lines in the file. My code :

with open("Result.txt") as f:
    for i, line in enumerate(f):
        with open(f"{i+1}.txt", "w") as g:
            g.write(line)
Reply
#4
enumerate takes optional start argument. So you can write
with open("Result.txt") as f:
    for i, line in enumerate(f, start=1):
        with open(f"{i}.txt", "w") as g:
            g.write(line)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Correct/proper way to create save files snakes 0 474 Mar-11-2025, 06:58 PM
Last Post: snakes
  subprocess check_output text cut off Axel_Erfurt 5 792 Feb-20-2025, 02:15 PM
Last Post: DeaD_EyE
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 28,115 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  Python - Hidden Text / Html Mail python1337 1 2,957 Feb-08-2025, 10:47 AM
Last Post: python1337
  How to write variable in a python file then import it in another python file? tatahuft 4 953 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Problems writing a large text file in python Vilius 4 1,011 Dec-21-2024, 09:20 AM
Last Post: Pedroski55
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 1,052 Nov-21-2024, 11:48 PM
Last Post: haihal
  parsing a tree of text first the right most aligned blocks of text and so on arvindikchari 2 792 Nov-21-2024, 01:42 AM
Last Post: BashBedlam
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,552 Oct-17-2024, 01:15 AM
Last Post: Winfried
  Paste text with caret already positioned inside a placeholder; Wehaveall 1 838 Oct-15-2024, 10:28 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020