5

I have been successful in finding code for spawning a vim editor and creating a tempfile from a python script. The code is here, I found it here: call up an EDITOR (vim) from a python script

import sys, tempfile, os
from subprocess import call

EDITOR = os.environ.get('EDITOR','vim') 

initial_message = "" 

with tempfile.NamedTemporaryFile(suffix=".tmp") as tempfile:
  tempfile.write(initial_message)
  tempfile.flush()
  call([EDITOR, tempfile.name])

The problem I having is that I cannot access the contents of the tempfile after I quit the editor.

tempfile
<closed file '<fdopen>', mode 'w+b' at 0x87c47b0>

tempfile.readline()

I get

ValueError: I/O operation on closed file

I did:

myfile = open(tempfile.name)
IOError: [Errno 2] No such file or directory: '/tmp/tmp7VKzfl.tmp'

How would I access the file in a python script once it has been edited with the editor?

Thank you

Community
  • 1
  • 1
Neeran
  • 1,753
  • 3
  • 21
  • 26

3 Answers3

5

Everything inside a with block is scoped. If you create the temporary file with the with statement, it will not be available after the block ends.

You need to read the tempfile contents inside the with block, or use another syntax to create the temporary file, e.g.:

tempfile = NamedTemporaryFile(suffix=".tmp")
# do stuff
tempfile.close()

If you do want to automatically close the file after your block, but still be able to re-open it, pass delete=False to the NamedTemporaryFile constructor (else it will be deleted after closing):

with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as tempfile:

Btw, you might want to use envoy to run subprocesses, nice library :)

Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
3

I was running into the same problem and had the same question.

It just didn't feel like a best practice to NOT delete a temp file just so that it could be read. I found the following way to read what was written to an instance of a NamedTempFile after vim editing, read it, and retain the advantage of deleting the tempfile. (It's not temporary if it's not deleted on its own, right?!)

One must rewind the tempfile and then read it. I found the answer at: http://pymotw.com/2/tempfile/

import os
import tempfile
from subprocess import call

temp = tempfile.TemporaryFile()
try:
    temp.write('Some data')
    temp.seek(0)

    print temp.read()
finally:
    temp.close()

Here is the actual code I used in my script: import tempfile import os from subprocess import call

EDITOR = os.environ.get('EDITOR', 'vim')
initial_message = "Please edit the file:"

with tempfile.NamedTemporaryFile(suffix=".tmp") as tmp:
    tmp.write(initial_message)
    tmp.flush()
    call([EDITOR, tmp.name])
    #file editing in vim happens here
    #file saved, vim closes
    #do the parsing with `tempfile` using regular File operations
    tmp.seek(0)
    print tmp.read()
dmmfll
  • 2,666
  • 2
  • 35
  • 41
2

NamedTemporaryFile creates a file that is deleted after it is closed (docs). It is therefore not suitable for when you need to write something to a temp file and the read the contents after the file is closed.

Use mkstemp instead (docs):

f, fname = mkstemp(suffix=".tmp")
f.write("...")
f.close()
call([EDITOR, fname])
codeape
  • 97,830
  • 24
  • 159
  • 188
  • I wasn't aware of ``delete=False`` (see the accepted answer). I'll leave my answer anyway, since it shows another valid approach to solving the problem. – codeape Apr 11 '12 at 11:36