简体   繁体   English

在python脚本中运行linux子进程

[英]Running a linux subprocess in python script

I need to show coloured diff of two text files on linux from my python script, for which I am using vimdiff. 我需要从python脚本中显示Linux上两个文本文件的有色差异,为此我正在使用vimdiff。 I am able to run vimdiff using subprocess but I cant get control back to python script. 我可以使用子进程运行vimdiff,但无法将控制权返回给python脚本。 It stays in the vimdiff mode forever until I do Ctrl+Z. 它一直保持在vimdiff模式,直到我按Ctrl + Z。 What i want to do is that open vimdiff and get back to python script after receiving some kind of input from the keyboard. 我想做的是从键盘接收某种输入后,打开vimdiff并返回python脚本。 I tried signal module too but couldnt solve the problem. 我也尝试过信号模块,但无法解决问题。

I am a beginner in python so far what I tried is here: 我到目前为止是python的初学者,我在这里尝试过的是:

import subprocess
p = subprocess.Popen(["vimdiff","test1.txt", "test2.txt"])
try:
 p.wait()
except KeyboardInterrupt:
 p.kill()

Any help will be appreciated 任何帮助将不胜感激

As I known vim uses ncurses library. 我知道vim使用ncurses库。 For run ncurses based application use module pexpect : 对于基于运行ncurses的应用程序,请使用模块pexpect

import pexpect
child = pexpect.spawn("vim")
child.interact()

vim is a full screen text mode program that does tricks with screen. vim是一个全屏文本模式程序,可对屏幕进行欺骗。 Avoid piping its input/output. 避免管道输入/输出。 Better run 更好的运行

subprocess.call(["vimdiff", "test1.txt", "test2.txt"])

or simply 或简单地

os.system("vimdiff test1.txt test2.txt")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM