views:

180

answers:

2

Hi everyone,

I am working on python right now and i am little bit stuck in performing some tricks. I have web form with two options- File upload and textarea, i can easily pass file name with file upload options but have problem when it's textarea. Because when i use textarea then first i have to save values passed from textarea to some files and save it on the working directory. After that i can execute commandline argument and pass same saved filename name. For this problem i have to generate unique file first and save the values passed from textarea in it.

Can anybody give me some tips to solve my problem. Any algorithms, suggestions and lines of code are appreciated.

Thanks for your concern

+1  A: 

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

import uuid

unique_filename = uuid.uuid4()
EnigmaCurry
Should i again edit my question in more understanding way?
Sorry i am working on windows platform so dont know how to handle subprocess
@EnigmaCurry, uuid seems to create a long unique string. I dont think its better to have file name with long string and UUID, () in it.
+1  A: 

If you want to make temporary files in Python, there's a module called tempfile in Python's standard libraries. If you want to launch other programs to operate on the file, use tempfile.mkstemp() to create files, and os.fdopen() to access the file descriptors that mkstemp() gives you.

Incidentally, you say you're running commands from a Python program? You should almost certainly be using the subprocess module.

So you can quite merrily write code that looks like:

import subprocess
import tempfile
import os

(fd, filename) = tempfile.mkstemp()
try:
    tfile = os.fdopen(fd, "w")
    tfile.write("Hello, world!\n")
    tfile.close()
    subprocess.Popen(["/bin/cat", filename]).wait()        
finally:
    os.remove(filename)

Running that, you should find that the cat command worked perfectly well, but the temporary file was deleted in the finally block. Be aware that you have to delete the temporary file that mkstemp() returns yourself - the library has no way of knowing when you're done with it!

(Edit: I had presumed that NamedTemporaryFile did exactly what you're after, but that might not be so convenient - the file gets deleted immediately when the temp file object is closed, and having other processes open the file before you've closed it won't work on some platforms, notably Windows. Sorry, fail on my part.)

Richard Barrell
using **NamedTemporaryFile** is probably what they want (unless they want it to stay on the server, and then they can use "tempfile.NamedTemporaryFile(delete=False)")
Terence Honles
Can i make that temporary file name unique too ? so i can save it later when subprocess is completed with unique name
@Terence Honles: I'd suggested tempfile.NamedTemporaryFile() originally, but you can't really use that to make temporary files that other processes can access on Windows. NamedTemporaryFile(delete=False) certainly is *cleaner*, though.@user343934: tempfile.mkstemp() is guaranteed to give you a unique name each time it's called - it generates the names randomly and it uses the OS facilities (O_EXCL, if you're wondering) to avoid collisions.
Richard Barrell
wow I didn't know it doesn't work on windows... fail :( ...I guess that's good to know
Terence Honles
@Terence Honles: NamedTemporaryFile() doesn't actually fail on Windows (as far as I know), but you can't close file without deleting it too, and (as I understand file semantics on Windows) no other program can open the file while you have it open.I might be wrong; the semantics for having multiple processes sharing a file under Windows might have changed since I last checked.
Richard Barrell
@ Richard, it says access denied when it tried to provide "C:/wamp/www/project" instead of "/bin/cat"
@user343934 /bin/cat is the name of a program, which always exists on Unix. I put that in because I forgot that people use computers that aren't Macs ;)You could replace it with the name of a command that will word on Windows. For instance, replace the whole subprocess.Popen line with:` subprocess.Popen(["cmd.exe", "/c", "type", filename]).wait()`I believe that that will work, but I don't have a Windows box with Python handy to test with.
Richard Barrell