Python Forum
Increment text files output and limit contains - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Increment text files output and limit contains (/thread-32253.html)



Increment text files output and limit contains - Kaminsky - Jan-30-2021

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.


RE: Increment text files output and limit contains - bowlofred - Jan-30-2021

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)