[Solved] Append an Auto-Incremented number to the end of a file?


The answers that others have put forwards helped me to create a program that every time the .py file is called it counts the number of files in the sub-directory and adds it to a file name and creates the file

The Code I Used:

 path, dirs, files = next(os.walk("C:/xampp/htdocs/addfiles/text/"))
 file_count = len(files)
 save_path="C:/xampp/htdocs/addfiles/text/"
 name_of_file="text"
 completeName = os.path.join(save_path, name_of_file + str(file_count) + ".php")         

after running this 3 times i got:

['text0.php', 'text1.php', 'text2.php'] in the .../text directory 

if you want to create ‘text1.php’ first, instead of ‘text0.php’ then make a change to file_count

file_count = len(files) + 1

after running this three times you’ll get:

['text1.php', 'text2.php', 'text3.php']

i also found writing to this newly created file is really easy later in my code i add

page="content you want to add to this file"
file1 = open(completeName, 'w+')
csv_writer = csv.writer(file1)
csv_writer.writerow(page)

solved Append an Auto-Incremented number to the end of a file?