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!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Python has facilities to generate temporary file names, see http://docs.python.org/library/tempfile.html. For instance:
Each call to
tempfile.NamedTemporaryFile()results in a different temp file, and its name can be accessed with the.nameattribute, e.g.: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
deleteparameter is False, the file is notautomatically deleted.
Full parameter set:
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):
Additional examples for working with temporary files can be found here