在Python中生成随机文件名的最佳方法

问题:在Python中生成随机文件名的最佳方法

在Python中,生成一些随机文本以添加到我要保存到服务器的文件(名称)的一种好的方法或最佳方法是,只是确保它不会被覆盖。谢谢!

In Python, what is a good, or the best way to generate some random text to prepend to a file(name) that I’m saving to a server, just to make sure it does not overwrite. Thank you!


回答 0

Python具有生成临时文件名的功能,请参见http://docs.python.org/library/tempfile.html。例如:

In [4]: import tempfile

每次调用都会生成tempfile.NamedTemporaryFile()一个不同的临时文件,并且可以使用.name属性访问其名称,例如:

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'

一旦拥有唯一的文件名,它就可以像任何常规文件一样使用。注意:默认情况下,文件在关闭时将被删除。但是,如果delete参数为False,则不会自动删除文件。

完整的参数集:

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'

此处可以找到有关使用临时文件的其他示例。

Python has facilities to generate temporary file names, see http://docs.python.org/library/tempfile.html. For instance:

In [4]: import tempfile

Each call to tempfile.NamedTemporaryFile() results in a different temp file, and its name can be accessed with the .name attribute, e.g.:

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'

Once you have the unique filename it can be used like any regular file. Note: By default the file will be deleted when it is closed. However, if the delete parameter is False, the file is not automatically deleted.

Full parameter set:

tempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])

it is also possible to specify the prefix for the temporary file (as one of the various parameters that can be supplied during the file creation):

In [9]: tf = tempfile.NamedTemporaryFile(prefix="zz")
In [10]: tf.name
Out[10]: 'c:\\blabla\\locals~1\\temp\\zzrc3pzk'

Additional examples for working with temporary files can be found here


回答 1

您可以使用UUID模块生成随机字符串:

import uuid
filename = str(uuid.uuid4())

鉴于UUID生成器极不可能产生重复的标识符(在这种情况下为文件名),因此这是一个有效的选择:

仅在接下来的100年中每秒生成10亿个UUID之后,仅创建一个副本的可能性就约为50%。如果地球上每个人都拥有6亿个UUID,则重复一次的可能性约为50%。

You could use the UUID module for generating a random string:

import uuid
filename = str(uuid.uuid4())

This is a valid choice, given that an UUID generator is extremely unlikely to produce a duplicate identifier (a file name, in this case):

Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.


回答 2

一种常见的方法是在文件名中添加时间戳作为前缀/后缀,以使其与文件具有某种时间关系。如果您需要更多的唯一性,您仍然可以在其中添加随机字符串。

import datetime
basename = "mylogfile"
suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
filename = "_".join([basename, suffix]) # e.g. 'mylogfile_120508_171442'

a common approach is to add a timestamp as a prefix/suffix to the filename to have some temporal relation to the file. If you need more uniqueness you can still add a random string to this.

import datetime
basename = "mylogfile"
suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
filename = "_".join([basename, suffix]) # e.g. 'mylogfile_120508_171442'

回答 3

OP要求创建随机文件名而不是随机文件。时间和UUID可能会发生冲突。如果您在单台机器(不是共享文件系统)上工作,并且进程/线程不会踩踏自身,请使用os.getpid()获取自己的PID,并将其用作唯一文件名的元素。其他进程显然不会获得相同的PID。如果您是多线程的,请获取线程ID。如果在代码的其他方面,单个线程或进程可能会生成多个不同的临时文件,则可能需要使用另一种技术。滚动索引可以正常工作(如果您没有将它们保持太久或使用太多文件,您将担心滚动)。在这种情况下,将全局散列/索引保留为“活动”文件就足够了。

对于冗长的解释,我们深表歉意,但这确实取决于您的确切用法。

The OP requested to create random filenames not random files. Times and UUIDs can collide. If you are working on a single machine (not a shared filesystem) and your process/thread will not stomp on itselfk, use os.getpid() to get your own PID and use this as an element of a unique filename. Other processes would obviously not get the same PID. If you are multithreaded, get the thread id. If you have other aspects of your code in which a single thread or process could generate multiple different tempfiles, you might need to use another technique. A rolling index can work (if you aren’t keeping them so long or using so many files you would worry about rollover). Keeping a global hash/index to “active” files would suffice in that case.

So sorry for the longwinded explanation, but it does depend on your exact usage.


回答 4

如果您不需要文件路径,而只需要具有预定义长度的随机字符串,则可以使用类似的内容。

>>> import random
>>> import string

>>> file_name = ''.join(random.choice(string.ascii_lowercase) for i in range(16))
>>> file_name
'ytrvmyhkaxlfaugx'

If you need no the file path, but only the random string having predefined length you can use something like this.

>>> import random
>>> import string

>>> file_name = ''.join(random.choice(string.ascii_lowercase) for i in range(16))
>>> file_name
'ytrvmyhkaxlfaugx'

回答 5

如果要将原始文件名保留为新文件名的一部分,则可以使用当前时间的MD5哈希值来生成统一长度的唯一前缀:

from hashlib import md5
from time import localtime

def add_prefix(filename):
    prefix = md5(str(localtime()).encode('utf-8')).hexdigest()
    return f"{prefix}_{filename}"

调用add_prefix(’style.css’)会生成如下序列:

a38ff35794ae366e442a0606e67035ba_style.css
7a5f8289323b0ebfdbc7c840ad3cb67b_style.css

If you want to preserve the original filename as a part of the new filename, unique prefixes of uniform length can be generated by using MD5 hashes of the current time:

from hashlib import md5
from time import localtime

def add_prefix(filename):
    prefix = md5(str(localtime()).encode('utf-8')).hexdigest()
    return f"{prefix}_{filename}"

Calls to the add_prefix(‘style.css’) generates sequence like:

a38ff35794ae366e442a0606e67035ba_style.css
7a5f8289323b0ebfdbc7c840ad3cb67b_style.css

回答 6

在这里加两分钱:

In [19]: tempfile.mkstemp('.png', 'bingo', '/tmp')[1]
Out[19]: '/tmp/bingoy6s3_k.png'

根据tempfile.mkstemp的python文档,它将以最安全的方式创建一个临时文件。请注意,该文件将在调用后存在:

In [20]: os.path.exists(tempfile.mkstemp('.png', 'bingo', '/tmp')[1])
Out[20]: True

Adding my two cents here:

In [19]: tempfile.mkstemp('.png', 'bingo', '/tmp')[1]
Out[19]: '/tmp/bingoy6s3_k.png'

According to the python doc for tempfile.mkstemp, it creates a temporary file in the most secure manner possible. Please note that the file will exist after this call:

In [20]: os.path.exists(tempfile.mkstemp('.png', 'bingo', '/tmp')[1])
Out[20]: True

回答 7

我个人更希望文本不仅是随机的/唯一的,而且也是美丽的,这就是为什么我喜欢hashids lib的原因,它可以从整数生成美观的随机文本。可以通过安装

pip install hashids

片段:

import hashids
hashids = hashids.Hashids(salt="this is my salt", )
print hashids.encode(1, 2, 3)
>>> laHquq

简短的介绍:

Hashids是一个小型的开放源代码库,可从数字生成短的,唯一的,非顺序的ID。

I personally prefer to have my text to not be only random/unique but beautiful as well, that’s why I like the hashids lib, which generates nice looking random text from integers. Can installed through

pip install hashids

Snippet:

import hashids
hashids = hashids.Hashids(salt="this is my salt", )
print hashids.encode(1, 2, 3)
>>> laHquq

Short Description:

Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers.


回答 8

>>> import random
>>> import string    
>>> alias = ''.join(random.choice(string.ascii_letters) for _ in range(16))
>>> alias
'WrVkPmjeSOgTmCRG'

您可以将’string.ascii_letters’更改为任何字符串格式,以生成任何其他文本,例如移动NO,ID …

>>> import random
>>> import string    
>>> alias = ''.join(random.choice(string.ascii_letters) for _ in range(16))
>>> alias
'WrVkPmjeSOgTmCRG'

You could change ‘string.ascii_letters’ to any string format as you like to generate any other text, for example mobile NO, ID…


回答 9

import uuid
   imageName = '{}{:-%Y%m%d%H%M%S}.jpeg'.format(str(uuid.uuid4().hex), datetime.now())
import uuid
   imageName = '{}{:-%Y%m%d%H%M%S}.jpeg'.format(str(uuid.uuid4().hex), datetime.now())

回答 10

您可以使用随机包:

import random
file = random.random()

You could use the random package:

import random
file = random.random()