解决方案取决于您拥有的编辑器,编辑器可能找到的环境变量以及编辑器是否采用任何命令行参数.
这是一个简单的解决方案,适用于Windows,没有任何环境变量或编辑器的命令行参数.根据需要进行修改.
import subprocess
import os.path
def start_editor(editor,file_name):
if not os.path.isfile(file_name): # If file doesn't exist, create it
with open(file_name,'w'):
pass
command_line=editor+' '+file_name # Add any desired command line args
p = subprocess.Popen(command_line)
p.wait()
file_name='test.txt' # Probably known from elsewhere
editor='notepad.exe' # Read from environment variable if desired
start_editor(editor,file_name)
with open(file_name,'r') as f: # Do something with the file, just an example here
for line in f:
print line