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
#1
Hi everyone,

I am coming to you because I need help with the code below.

I'm looking to generate text files of n- lines. Each line contains n- random characters.
I also want the names of the output files to increment when the n- lines are reached.

The number of files is not defined, and the process will be killed by "ctrl+c".
For my example I chose files of 1000 lines, each containing 6 characters.
Generate a 1000 lines file, no problem.
I tried several loops in several places, but I get stuck and I can't get what I want.

The "count" variable allows me to determine the number of lines saved in the file.
The "nameFile" variable is for incrementing the filename.

I tried replacing the "while" by "if" but identical.

count = 0
while True:
    letters_and_digits = string.ascii_letters + string.digits
    result_str = ''.join((random.choice(letters_and_digits) for i in range(6)))
    nameFile = 0
    while count < 10000:
        with open("test%d.txt" % nameFile, "a") as f:
            f.write(result_str + '\n')
        count += 1
I always come back to my code that works without being able to advance it.

Thank you for your feedback.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  azure TTS from text files to mp3s mutantGOD 2 1,635 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  merge two csv files into output.csv using Subprocess mg24 9 1,687 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  help to increment a third list hermine 7 1,269 Nov-29-2022, 04:19 PM
Last Post: perfringo
  mysql id auto increment not working tantony 10 2,304 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Writing into 2 text files from the same function paul18fr 4 1,628 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,510 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,125 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,841 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,474 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Search text in PDF and output its page number. atomxkai 21 8,670 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