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
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 678 Yesterday, 07:07 AM
Last Post: Bronjer
Brick Number stored as text with openpyxl CAD79 2 380 Apr-17-2024, 10:17 AM
Last Post: CAD79
  Sending a text from Python sawtooth500 2 159 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  very newbie problem on text file zapad 2 197 Apr-12-2024, 06:50 PM
Last Post: zapad
  help with scrolling text on RGB Matrix Foutsy 3 254 Apr-09-2024, 09:00 PM
Last Post: deanhystad
  Text parsing Arik 5 381 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  replace text in a txt cartonics 19 2,215 Jan-30-2024, 06:58 AM
Last Post: Athi
  Open/save file on Android frohr 0 315 Jan-24-2024, 06:28 PM
Last Post: frohr
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Text conversion to lowercase is not working ineuw 3 460 Jan-16-2024, 02:42 AM
Last Post: ineuw

Forum Jump:

User Panel Messages

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