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
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.
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting and Merging text-files AlphaInc 10 641 Aug-20-2021, 05:42 PM
Last Post: snippsat
  Replace String in multiple text-files [SOLVED] AlphaInc 5 668 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Several pdf files to text mfernandes 10 1,080 Jul-07-2021, 11:39 PM
Last Post: Pedroski55
  Open and read multiple text files and match words kozaizsvemira 3 4,248 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Reading Multiple text Files in pyhton Fatim 1 433 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Character Increment AnokhiRaaz 1 797 Apr-22-2021, 04:29 AM
Last Post: buran
  Getting a GET request output text into a variable to work with it. LeoT 2 585 Feb-24-2021, 02:05 PM
Last Post: LeoT
  Split gps files based on time (text splitting) dervast 0 523 Nov-09-2020, 09:19 AM
Last Post: dervast
  Searching for specific word in text files. JellyCreeper6 1 607 Nov-03-2020, 01:52 PM
Last Post: DeaD_EyE
  opening files and output of parsing leodavinci1990 4 870 Oct-12-2020, 06:52 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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