hunch hunch - 6 months ago 8
Python Question

How to remain in python interactive interpreter after running program?

I am running python program from terminal

python -i abc.py <test.txt


but after completion of the program it does not remain in python.

what I want:

Output:
4 21
>>>


What is happening--

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


Usually if we give command
python -i abc.py
it goes into python interactive mode after running the program.

Program(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


Thanks

Answer

You redirect the stdin, so when the file ends, the process exits.

What you is read from the file first, then from stdin:

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

Without any arguments, cat reads from stdin. So in this case, the python process first receives this output of cat test.txt and then the output of just cat, which is stdin.

Note that this does not behave exactly the same as using python -i abc.py without the redirect.

Comments