I've created a system where users upload videos to my server's filesystem and I need a way to generate unique filenames for the video files. Should I be using random.getrandbits? Is there a way to do this with a combination of letters and numbers?
-
possible duplicate of http://stackoverflow.com/questions/2257441/python-random-string-generation-with-upper-case-letters-and-digits – Michael Mior Jan 17 '12 at 16:53
-
3What framework are you using? Often this is handled for you. What's wrong with the existing http://docs.python.org/library/tempfile.html library? – S.Lott Jan 17 '12 at 16:53
-
@S.Lott I don't think he wants to store the videos temporarily. – Gandaro Jan 17 '12 at 17:31
-
There's nothing "temporary" about the file names. They can be outside the `/tmp` filesystem. – S.Lott Jan 17 '12 at 18:00
-
@S.Lott: by default temporary files will be deleted when closed. You would have to use `NamedTemporaryFile` and pass `delete=False`. Either way I don't think it's very pythonic to use `tempfile` for files that are not temporary. – Rob Wouters Jan 17 '12 at 18:04
-
@RobWouters: Totally Correct. That doesn't explain why it isn't being used in the first place. The question doesn't make much sense consider that the required capability is already part of the Python library. – S.Lott Jan 17 '12 at 18:06
-
1I actually do want to store the videos temporarily...they should be able to be previewed (played back), then uploaded to youtube, then the original video should be deleted. – zakdances Jan 17 '12 at 22:01
3 Answers
This is what I use:
import base64
import uuid
base64.urlsafe_b64encode(uuid.uuid4().bytes)
I generate a uuid, but I use the bytes instead of larger hex version or one with dashes. Then I encode it into a URL-safe base-64 string. This is a shorter length string than using hex, but using base64 makes it so that the characters in the string are safe for files, urls and most other things.
One problem is that even with the urlsafe_b64encode, it always wants to '='s signs onto the end which are not so url-safe. The '='s signs are for decoding the base-64 encoded information, so if you are only trying to generate random strings, then you can probably safey remove them with:
str.replace('=', '')

- 48,402
- 65
- 188
- 258
-
2This is EXACTLY what I needed right now; thanks! Just thought you'd want to know that your answer to a low-scoring question still helped a random guy on the Internet. – Graham Mitchell Jul 20 '13 at 22:49
-
Thanks for the feedback. For us, its held up to the test of time so far, its almost a year later we still use this little snippit of code to generate primary keys throughout our entire application. – Chris Dutrow Jul 21 '13 at 18:33
-
Noting that on Python 3 I had to include `.decode('utf-8')` to get a string to do replacement on: `base64.urlsafe_b64encode(uuid.uuid4().bytes).decode('utf-8').replace('=', '')` – Jacob Davis-Hansson May 18 '17 at 14:11
You can use, in Python 3.6, the secrets module.
>>> import secrets
>>> secrets.token_urlsafe(8)
'Pxym7N2zJRs'
Further documentation is here: https://docs.python.org/3/library/secrets.html

- 314
- 9
- 16
Take a look at Universally unique identifiers (UUID)
and Python's module uuid
.

- 15,797
- 3
- 42
- 36