#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0

    Reading standard input...how to stop?


    hi, I've trouble reading standard input in python. For example.if I said

    for a in stdin:
    blablabla
    bla bla bal

    then, when I run my program, it keep asking me to input value...but what if I finished inputing my value? how to stop?
    *I try Ctrl-C, but it end up stop the program and didn't display the results.*
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    CTRL-Z on Windows

    CTRL-D on Unix/Linux.

    These signal 'end of file' to the OS.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    okay,

    I'm not sure if it is normal that I have to press Ctrl-Z TWO times to stop the program...instead of one. Is that normal?

    My standard input is like something:

    for a in sys.stdin:
    bla blab bla
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just to alleviate any doubts about this. You should have to press Ctrl-Z once to stop reading stdin and once to exit the program if there is still work to be done, otherwise the program should get to the end and exit. You would have to press it again if for example you were inside an infinite loop.

    The following program requires you to press Crtl-D once and then exits after printing what was typed.

    Code:
    #!/usr/bin/env python
    
    from sys import stdin
    
    for line in stdin:
        print 'STDIN:', line,
    Mark-Smiths-Computer:~ Mark$ ~/Desktop/test.py
    hello
    STDIN: hello
    Mark-Smiths-Computer:~ Mark$
    Hope this clears everything up,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    oh i see,

    yeah, my program is quite big. So the first time is only stop the stdin and the second time is stop the program and it show the results after...

IMN logo majestic logo threadwatch logo seochat tools logo