Jan-30-2021, 06:58 PM
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
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)