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 ;)

Yashar Aliabbasi
  • 2,663
  • 1
  • 23
  • 35
  • Could you prefix the UID with a number? – sebastian-c Oct 03 '17 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. – deceze Oct 03 '17 at 11:41
  • What's wrong with using a counter? – Lafexlos Oct 03 '17 at 12:12
  • @Lafexlos think about deleting and creating – Yashar Aliabbasi Oct 03 '17 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? – Yashar Aliabbasi Oct 03 '17 at 17:21
  • I did'nt got the reason of down vote reason.please tell me...! – Yashar Aliabbasi Oct 03 '17 at 17:26
  • That’s not a counter then, that’s *counting the present files and adding one*. A counter is a number you store somewhere and that you only ever keep increasing. – deceze Oct 03 '17 at 18:48
  • @deceze I know what you mean.but you didn't get my example's point.in that case we can have 2 or more file with same index – Yashar Aliabbasi Oct 04 '17 at 08:25
  • No. How? How do you get two files with the same number if you're using a counter that's never decreasing? You create one file, you call it "1", you save the number "1" somewhere (your counter). You create a second file, you increase the counter, save "2". Repeat… You delete a file, your counter is at, say, "5". You create another file "6"… – deceze Oct 04 '17 at 08:29
  • @deceze think of overflowing pal.and [NEWSEQUENTIALID](https://learn.microsoft.com/en-us/sql/t-sql/functions/newsequentialid-transact-sql).But thank you for responding. – Yashar Aliabbasi Oct 04 '17 at 12:02

3 Answers3

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.

frarugi87
  • 2,826
  • 1
  • 20
  • 41
  • 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 – Yashar Aliabbasi Oct 03 '17 at 17:25
  • then please add more details about the current requirements. Don't you have any source for the time? RTC, GPS, internet.... – frarugi87 Oct 03 '17 at 19:01
  • Think of lightweight Debian OS without any source for date/time supplier and every time device switch off all date/time will be restart – Yashar Aliabbasi Oct 04 '17 at 05:27
  • @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) – frarugi87 Oct 04 '17 at 07:09
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.

Rafael Santos
  • 21
  • 1
  • 2
2

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

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98