If I understood correctly, this is an example of XY Problem. What you want is that the filenames are unique and incremental (after the first one comes the second and so on).
This means you don't want a random file name, only one that is not reused.
I suggest a scheme such as
YYYYMMDD_HHmmSS_IDX
where YYYY
is the year, MM
is the month, DD
the day, HH
the hour (24-based), mm
the minute, SS
the seconds; all this data represent the start time of the instance of the program (so at the beginning you initialize a variable at the current time and never update it). All of these data are 0-padded if needed (for instance, if it is 9:08 in the morning the field will be 09 and 08).
The IDX
field is an incremental index (for instance, a 0-padded 3-digits index) which gets updated every time you need to create a new file (000 at the beginning, 001 after, and so on).
This will be incremental, and moreover store also some other info (e.g. the start time of the date). It will be unique, unless you start two instances the very same second (then you can also add a millisecond counter)
EDIT:
If you don't have any means of getting the time (no RTC, no GPS, no internet, nothing) you just have to decide a pattern with a counter, for instance:
myfile_XXXXXX
where XXXXXX
is a number with leading zeros and enough digits to let you never exceed the maximum.
You then have two options: list all files, pick the last and increment, or store the previous value of the counter.
In the first case you have some options. For instance, this answer uses glob
(you just have to sort it, using for instance this syntax: sorted(glob.glob('/path/to/files/myfile_*')). This returns you a list; pick the last, remove the first part in order to have just the number, and increment it.
The second way is easier but requires you to create another file. In this file you simply store the current number, and save it back whenever you create a new file. You can even make it a settings file (where you also store the path to the files, and various options). If you want to put more info than just the number, think of using a more structured file such as an XML or a JSON file.
The first way is less error prone and more automatic. The second is easier to implement.