tempfile Question

Gregory Piñero gregpinero at gmail.com
Tue Jun 6 17:50:46 EDT 2006


Hey group,

I have a command line tool that I want to be able to call from a
Python script.  The problem is that this tool only writes to a file.

So my solution is to give the tool a temporary file to write to and
then have Python read that file.  I figure that's the safest way to
deal with this sort of thing.  (But I'm open to better methods).

Here's my code so far, could anyone tell me the proper way to use
tempfile.  This code won't let the tool write to the file because
Python has it locked.  But I'm worried that if I close the file then
windows might take it away?  I have no idea.

<code>
PDFTOTEXTPATH=r'C:\xpdf-3.01pl2-win32\xpdf-3.01pl2-win32\pdftotext.exe'

def read_pdf_text(filepath):
    outfile=tempfile.NamedTemporaryFile()
    outfilename=outfile.name
    command=r'"%s" "%s" "%s"' % (PDFTOTEXTPATH,filepath,outfilename)
    result=os.popen(command).read()
    pdftext=outfile.read()
    outfile.close()
    return pdftext
</code>

Much thanks!

-- 
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)



More information about the Python-list mailing list