Python Forum

Full Version: New .txt file Generation in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to create a new .txt file every time i run the python script. Any idea how to do that . My current code :

def write_up_commands(up_int):
    fp = open('./abc.txt', 'a+')
    for i in up_int:
        fp.write('int ' + i + '\n mode \n')
    fp.close()
something like

1_abc
2_abc
3_abc
can i add a date/timestamp ?
If you want to have a new numeric index, you would use os.listdir to get all the current files, split('-') to get the indexes from the current files, and then max() + 1 of that gives you the new index for the new file.

If you want to have a date/timestamp, you would use the datetime module to get that, and just add it to the file name.