1

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

测试.txt

1
I want to buy Car for $10000

谢谢

4

1 回答 1

3

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

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

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

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

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

于 2016-06-02T10:38:12.947 回答