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
Brick Number stored as text with openpyxl CAD79 1 215 Mar-19-2024, 08:31 PM
Last Post: deanhystad
  Text parsing Arik 5 306 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  replace text in a txt cartonics 19 2,072 Jan-30-2024, 06:58 AM
Last Post: Athi
  Open/save file on Android frohr 0 278 Jan-24-2024, 06:28 PM
Last Post: frohr
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Text conversion to lowercase is not working ineuw 3 398 Jan-16-2024, 02:42 AM
Last Post: ineuw
  Script that alternates between 2 text messages DiscoMatic 1 492 Dec-12-2023, 03:02 PM
Last Post: buran
  Decryption not working if key has same symbol like text Paragoon2 0 292 Nov-11-2023, 09:32 PM
Last Post: Paragoon2
  write to csv file problem jacksfrustration 11 1,370 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,308 Nov-09-2023, 10:56 AM
Last Post: mg24

Forum Jump:

User Panel Messages

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