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

时间:2022-09-15 20:08:07

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!

在Python中,生成一些随机文本以预先添加到我保存到服务器的文件(名称)的最佳方法是什么,只是为了确保它不会覆盖。谢谢!

9 个解决方案

#1


84  

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

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

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.:

每次调用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'

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.

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

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

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

#2


72  

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

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

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

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

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.

只有在未来100年内每秒产生10亿UUID之后,仅创建一个副本的概率大约为50%。如果地球上的每个人拥有6亿UUID,则一次重复的概率约为50%。

#3


9  

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'

#4


5  

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.

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

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

很抱歉这个长篇大论的解释,但这取决于你的确切用法。

#5


4  

If you want to preserve the original file name as a part of the new filename, unique prefixes of unifom length can be generted by:

如果要将原始文件名保留为新文件名的一部分,则可以通过以下方式生成unifom长度的唯一前缀:

def add_prefix(filename):

  from hashlib import md5
  from time import localtime

  return "%s_%s" % (md5(str(localtime())).hexdigest(), filename)

Calls to the àdd_prefix('style.css') generates sequence like:

调用àdd_prefix('style.css')会生成如下序列:

a38ff35794ae366e442a0606e67035ba_style.css
7a5f8289323b0ebfdbc7c840ad3cb67b_style.css

#6


2  

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

#7


1  

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:

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

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

#8


0  

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

我个人更喜欢让我的文本不仅是随机/唯一而且非常漂亮,这就是我喜欢hashids lib的原因,它可以从整数中生成漂亮的随机文本。可以通过安装

pip install hashids

pip安装哈希

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.

Hashids是一个小型开源库,可以从数字中生成简短,独特,非顺序的ID。

#9


-1  

You could use the random package:

你可以使用随机包:

import random
file = random.random()

#1


84  

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

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

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.:

每次调用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'

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.

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

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

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

#2


72  

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

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

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

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

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.

只有在未来100年内每秒产生10亿UUID之后,仅创建一个副本的概率大约为50%。如果地球上的每个人拥有6亿UUID,则一次重复的概率约为50%。

#3


9  

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'

#4


5  

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.

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

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

很抱歉这个长篇大论的解释,但这取决于你的确切用法。

#5


4  

If you want to preserve the original file name as a part of the new filename, unique prefixes of unifom length can be generted by:

如果要将原始文件名保留为新文件名的一部分,则可以通过以下方式生成unifom长度的唯一前缀:

def add_prefix(filename):

  from hashlib import md5
  from time import localtime

  return "%s_%s" % (md5(str(localtime())).hexdigest(), filename)

Calls to the àdd_prefix('style.css') generates sequence like:

调用àdd_prefix('style.css')会生成如下序列:

a38ff35794ae366e442a0606e67035ba_style.css
7a5f8289323b0ebfdbc7c840ad3cb67b_style.css

#6


2  

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

#7


1  

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:

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

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

#8


0  

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

我个人更喜欢让我的文本不仅是随机/唯一而且非常漂亮,这就是我喜欢hashids lib的原因,它可以从整数中生成漂亮的随机文本。可以通过安装

pip install hashids

pip安装哈希

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.

Hashids是一个小型开源库,可以从数字中生成简短,独特,非顺序的ID。

#9


-1  

You could use the random package:

你可以使用随机包:

import random
file = random.random()