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


Messages In This Thread
Increment text files output and limit contains - by Kaminsky - Jan-30-2021, 05:57 PM

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