我最初的目标是通过Python脚本中的命令行文本编辑器获取用户输入。更具体地说,我的计划是创建一个临时文件并使用一些预先写好的文本填充它,使用文本编辑器打开文件,并允许用户修改文件内容,在用户退出后从文件中读取数据编辑器,然后在完成后最后删除文件。使用Python子进程中的Vim编辑临时文件在Mac OS上无法按预期工作
我似乎找到了一种方法可以做到这一点,但我一直在尝试一些方法,但没有奏效,我想明白为什么。
考虑以下Python脚本(从this post拍摄脚本的稍作修改的版本):
#!/usr/bin/env python2
# -*- encoding: ascii -*-
"""callvim.py
Demonstrates calling a text-editor (e.g. Vim) from within a Python script,
including passing input to the editor and reading output from the editor.
"""
import tempfile
import os
from subprocess import call
# Get the text editor from the shell, otherwise default to Vim
EDITOR = os.environ.get('EDITOR','vim')
# Set initial input with which to populate the buffer
initial_message = "Hello world!"
# Open a temporary file to communicate through
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
# Write the initial content to the file I/O buffer
tf.write(initial_message)
# Flush the I/O buffer to make sure the data is written to the file
tf.flush()
# Open the file with the text editor
call([EDITOR, tf.name])
# Rewind the file offset to the beginning of the file
tf.seek(0)
# Read the file data into a variable
edited_message = tf.read()
# Output the data
print(edited_message)
我试过至今运行在两个不同的环境中,此脚本:MacOS的计算机上(运行MacOS的10.12)和Debian计算机(运行Debian 8.8)。这两台电脑都安装了相同(小)版本的Vim(Vim 7.4)。
当我在我的Debian 8(Jessie)机器上运行EDITOR=vim
这个脚本时,它按预期工作。我得到Vim提示和一个包含字符串“Hello world!”的缓冲区。在编辑缓冲区以包含字符串“Goodbye world!”,保存文件并退出Vim之后,我看到字符串“Goodbye world!”。打印到控制台。
当我在我的macOS 10.12(Sierra)机器上运行相同的脚本时,它似乎不起作用。相同的程序导致“Hello world!”显示在屏幕上 - 就像文件在被编辑之前正在被读取一样。
但是,如果在我的Mac上运行脚本EDITOR=nano
然后再次一切似乎按预期工作。
我尝试了使用tempfile
模块中不同方法(例如使用tempfile.TemporaryFile()
和tempfile.mkstemp()
)的相同结果对此脚本进行了一些更改。
现在考虑下面的替代文字:
#!/usr/bin/env python2
# -*- encoding: ascii -*-
"""callvim.py
Demonstrates calling a text-editor (e.g. Vim) from within a Python script,
including passing input to the editor and reading output from the editor.
"""
import subprocess
import os
# Create a temporary file and write some default text
file_path = "tempfile"
file_handle = open(file_path, "w")
file_handle.write("Hello world!")
file_handle.close()
# Open the file with Vim
subprocess.call(["vim", file_path])
# Rewind to the beginning of the file
file_handle = open(file_path, 'r')
# Read the data from the file
data = file_handle.read()
# Close the temporary file
file_handle.close()
# Delete the temporary file
os.remove(file_path)
# Print the data
print(data)
这个脚本,从而避免了使用tempfile
模块,似乎这两个平台的一贯工作。
因此,看起来这个脚本可能由于某种原因而无法处理Vim和Python模块如何在macOS上进行交互。这里发生了什么?