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

时间:2016-06-02 09:54:54

标签: python python-interactive

我正在从终端运行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 $10000

由于

1 个答案:

答案 0 :(得分:3)

您重定向stdin,因此当文件结束时,进程退出。

首先从文件中读取什么,然后从stdin中读取:

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

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

请注意,这与使用python -i abc.py而没有重定向的行为完全相同。