Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Best way to generate random file names in Python
- In [4]: import tempfile
- In [5]: tf = tempfile.NamedTemporaryFile()
- In [6]: tf.name
- Out[6]: 'c:\blabla\locals~1\temp\tmptecp3i'
- In [7]: tf = tempfile.NamedTemporaryFile()
- In [8]: tf.name
- Out[8]: 'c:\blabla\locals~1\temp\tmpr8vvme'
- tempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])
- In [9]: tf = tempfile.NamedTemporaryFile(prefix="zz")
- In [10]: tf.name
- Out[10]: 'c:\blabla\locals~1\temp\zzrc3pzk'
- import uuid
- filename = str(uuid.uuid4())
- def add_prefix(filename):
- from hashlib import md5
- from time import localtime
- return "%s_%s" % (md5(str(localtime())).hexdigest(), filename)
- a38ff35794ae366e442a0606e67035ba_style.css
- 7a5f8289323b0ebfdbc7c840ad3cb67b_style.css
- import datetime
- basename = "mylogfile"
- suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
- filename = "_".join([basename, suffix]) # e.g. 'mylogfile_120508_171442'
Advertisement
Add Comment
Please, Sign In to add comment