Skip to main content
 首页 » 编程设计

python之从外部程序获取数据

2025年05月04日19youxin

我需要一种从外部编辑器获取数据的方法。

def _get_content(): 
     from subprocess import call 
     file = open(file, "w").write(some_name) 
     call(editor + " " + file, shell=True) 
     file.close() 
     file = open(file) 
     x = file.readlines() 
 
     [snip] 

个人认为应该有更优雅的方式。你看,我需要与外部编辑器交互并获取数据。

您知道更好的方法/有更好的想法吗?

编辑:

Marcelo 让我想到了使用 tempfile 来做到这一点。

这是我的做法:

def _tempfile_write(input): 
    from tempfile import NamedTemporaryFile 
 
    x = NamedTemporaryFile() 
    x.file.write(input) 
    x.close() 
    y = open(x) 
 
    [snip] 

这完成了工作,但也不是很令人满意。 听说过产卵吗?..

请您参考如下方法:

这是所有程序的方式,AFAIK。当然,我使用过的所有版本控制系统都会创建一个临时文件,将其传递给编辑器并在编辑器退出时检索结果,就像您所做的那样。