Python Forum
New .txt file Generation in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New .txt file Generation in Python
#1
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 ?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Node Flow Generation in Python Linenloid 0 657 Feb-21-2023, 07:09 PM
Last Post: Linenloid
  Allure Report Generation rotemz 0 793 Jan-24-2023, 08:30 PM
Last Post: rotemz
  Why doesnt chunk generation work? LotosProgramer 1 1,954 Apr-02-2022, 08:25 AM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,455 Oct-20-2021, 03:36 PM
Last Post: deanhystad
Question PDF generation / edit SpongeB0B 2 2,093 Jul-28-2021, 05:59 AM
Last Post: SpongeB0B
  Calling Input for Random Generation ScaledCodingWarrior 1 1,865 Feb-02-2021, 07:27 PM
Last Post: bowlofred
  Newbie question for complicated namedtuple generation zydjohn 0 2,178 Dec-16-2017, 09:50 AM
Last Post: zydjohn
  Understanding maze generation code kleynah22 1 3,258 Nov-11-2017, 02:09 PM
Last Post: sparkz_alot
  Unwanted random generation of scripted Shapes in GASP diemildefreude 3 5,184 Oct-23-2016, 03:11 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020