Python Forum
Increment text files output and limit contains
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Increment text files output and limit contains
#2
To me, your loops are backwards. You're creating the lines in the outer loop, but the files in the inner loop.

Your count variable is keeping track of the lines used in a file. But it's not set to zero for new files.

Opening a file takes a lot more work than writing to an already open file. You shouldn't be re-opening the file for each write you want to do. Open it once, do all the writes if possible, then close it or let it go out of scope.

The letters_and_digits is static, and should be done outside any loop. So a better overall flow would be similar to
character_choice = <whichever characters you want>
filename_base = "test"
filename_count = 0
lines_per_file = 1000
characters_per_line = 6
while True:
    with open(f"{filename_base}_{filename_count}", "w") as f:
        for _ in range(lines_per_file):
            print("".join(random.choices(character_choice, k=characters_per_line)), file=f)
tester_V likes this post
Reply


Messages In This Thread
RE: Increment text files output and limit contains - by bowlofred - Jan-30-2021, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  azure TTS from text files to mp3s mutantGOD 2 1,695 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  merge two csv files into output.csv using Subprocess mg24 9 1,780 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  help to increment a third list hermine 7 1,327 Nov-29-2022, 04:19 PM
Last Post: perfringo
  mysql id auto increment not working tantony 10 2,410 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Writing into 2 text files from the same function paul18fr 4 1,672 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,565 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,164 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,909 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,511 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Search text in PDF and output its page number. atomxkai 21 8,903 Jan-21-2022, 06:20 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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