运行程序后如何留在python交互式解释器中?

How to remain in python interactive interpreter after running program?

我是运行python来自终端的程序

python -i abc.py <test.txt

但程序完成后它不会留在python。

我想要的:

Output:
4 21
>>>

发生了什么--

Output:
4 21
>>> >>> 
Downloads:~$

通常如果我们发出命令 python -i abc.py 它在 运行 程序后进入 python 交互模式。

程序(abc.py);

line=[]
f=int(raw_input())
i=0
while(i<f):
    m=raw_input()
    line.append(m)
    i+=1

for i in line:
    ind=i.find('$')
    temp=''
    for j in i[ind+1:]:
        t=ord(j)        
        if((t>=48)&(t<=57)):temp=temp+j
        elif(t!=32): break

    temp='$'+str(int(temp))
    print(temp)

test.txt

1
I want to buy Car for 000

谢谢

您重定向标准输入,因此当文件结束时,进程退出。

首先从文件中读取的内容,然后从标准输入中读取的内容:

(cat test.txt && cat) | python -i abc.py

没有任何参数,cat 从标准输入读取。所以在这种情况下,python 进程首先接收 cat test.txt 的输出,然后是 cat 的输出,即标准输入。

请注意,这与在没有重定向的情况下使用 python -i abc.py 的行为并不完全相同。