Best way to generate random file names in Python

前端 未结 11 2087
囚心锁ツ
囚心锁ツ 2020-12-04 08:48

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

11条回答
  •  眼角桃花
    2020-12-04 09:39

    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
    

提交回复
热议问题