Python: How to create a unique file name?

MysticCodes picture MysticCodes · Jun 2, 2010 · Viewed 103.4k times · Source

I have a python web form with two options - File upload and textarea. I need to take the values from each and pass them to another command-line program. I can easily pass the file name with file upload options, but I am not sure how to pass the value of the textarea.

I think what I need to do is:

  1. Generate a unique file name
  2. Create a temporary file with that name in the working directory
  3. Save the values passed from textarea into the temporary file
  4. Execute the commandline program from inside my python module and pass it the name of the temporary file

I am not sure how to generate a unique file name. Can anybody give me some tips on how to generate a unique file name? Any algorithms, suggestions, and lines of code are appreciated.

Thanks for your concern

Answer

EnigmaCurry picture EnigmaCurry · Jun 2, 2010

I didn't think your question was very clear, but if all you need is a unique file name...

import uuid

unique_filename = str(uuid.uuid4())