saganism
saganism

Reputation: 1

Adding a sequential letter to the end of a filename in Python

I am attempting to write a script to name a file with the user's username, date, then unique identifier. The objective is for a network of users to have a file name generated for them in a standardized format in sequential order in a shared database. I am able to generate the username and date, but am having trouble assigning the identifier. I am thinking a sequential ascii_lowercase, but obviously I can not have a duplicate.

Examples: user doug saves 3 file edits on June 1, his files should be saved as doug060115a, doug060115b, doug060115c. user doug saves 1 more file edit on June 2, his file should now save as doug060215a.

So, the unique identifier at the end of the file name should start at a and if that is taken move on to b, then c, and so forth. If the username or date change the unique identifier should start over at a.

Upvotes: 0

Views: 181

Answers (2)

Alex Ivanov
Alex Ivanov

Reputation: 823

I would suggest doing that in a table containing columns:

user_id, user, date, sequential_letter

If today the user adds more files you will check what letters are in use and assign the next one. If today's date changes you will start the sequence all over again. Sequential numbers will probably suit better for this purpose. You can use SQLite. Python handles it very well. http://zetcode.com/db/sqlitepythontutorial/

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117866

You can use an offset of the ord and some slicing

def incrementName(s):
    return s[:-1] + chr(ord(s[-1]) + 1)

>>> incrementName('doug060115b')
'doug060115c'

Not sure what you want to do once you hit z (if you anticipate that case)

Upvotes: 1

Related Questions