1

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

hunch
  • 329
  • 1
  • 2
  • 10

1 Answers1

3

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.

Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • This does work, can you please explain in detail why redirecting the stdin results in process exit. Why your method is working. – hunch Jun 02 '16 at 11:02