Tôi đã thành công trong việc tìm kiếm mã để sinh ra một trình soạn thảo vim và tạo một tempfile từ một kịch bản lệnh python. Mã này là ở đây, tôi thấy nó ở đây: call up an EDITOR (vim) from a python scriptTrình biên tập Vim trong kịch bản python tempfile
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])
Vấn đề tôi gặp là tôi không thể truy cập vào các nội dung của tempfile sau khi tôi bỏ trình soạn thảo.
tempfile
<closed file '<fdopen>', mode 'w+b' at 0x87c47b0>
tempfile.readline()
tôi nhận được
ValueError: I/O operation on closed file
tôi đã làm:
myfile = open(tempfile.name)
IOError: [Errno 2] No such file or directory: '/tmp/tmp7VKzfl.tmp'
Làm thế nào tôi có thể truy cập các tập tin trong một kịch bản python khi nó đã được chỉnh sửa với trình biên tập?
Cảm ơn bạn
Thank you very much – Neeran