1

I'm working on project that need to make unique and incremental file names.I found UUID best way for making unique names but being incremental is already unanswered.We have such a situation that our list will recreate after reboot based on saved file.and creating new files by adding to previous list.
my tried program is simple:

import uuid
print(uuid.uuid4().hex)
print(uuid.uuid4().hex)
print(uuid.uuid4().hex)
print(uuid.uuid4().hex)

and sample output is this:

e497b7d7651341e0ac214fea8c408a7b
5d956a98724c459796ef1fce3818ff45
e60f2517ac1a430eab84768dae9e49a1
9df26e765dee4c3197c2f5a69e210046

(This result will totally different from run to run)
as you can see there is no guaranty that new UUID will be greater/smaller than previous one.

What I want is something like NEWSEQUENTIALID in Sql Server

Thank you in advance ;)

10
  • Could you prefix the UID with a number? Commented Oct 3, 2017 at 11:37
  • 2
    If you want a counter, then you certainly don't want a UUID. They're the exact opposite essentially. If you want a counter then use a counter and store it somewhere. Commented Oct 3, 2017 at 11:41
  • What's wrong with using a counter? Commented Oct 3, 2017 at 12:12
  • @Lafexlos think about deleting and creating Commented Oct 3, 2017 at 12:15
  • @deceze if you add 4 file then delete third file and then add another file??then we have two 4 number.am I wrong? Commented Oct 3, 2017 at 17:21

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm using this program on embeded device that have no way to save date and time like we can do on computer.then I cant use this great way in my program.but thank you for answring
then please add more details about the current requirements. Don't you have any source for the time? RTC, GPS, internet....
Think of lightweight Debian OS without any source for date/time supplier and every time device switch off all date/time will be restart
@YasharAliabasi I added another solution if you don't have the time available. This said, if you are making a logger maybe adding an RTC is useful (because data without timestamp is not so much useful)
2

Have you tried uuid.uuid1()?

It has a time component so the generated uuids can be ordered.

From the docs:

Generate a UUID from a host ID, sequence number, and the current time.

The default node parameter uses the MAC address of the machine, so multiple machines running your code at the same time could never generate the same uuid.

Comments

2

You can use a file for saving the last number. Next time you can read this file and ++.

4 Comments

you mean persist data on file?
Yes, You can save UUID on file.
@AhmadAghazadeh but then why use a uuid?
This would be helpful for testing purposes when we want to reduce the number of flaky tests and sort by UUIDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.