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

    Join Date
    Feb 2004
    Posts
    10
    Rep Power
    0

    python p.py < t.txt (reading from standard input)


    how can i do this operation...

    i've tried

    import sys

    sys.stdout.write(sys.stdin.read())


    but it's not accomplishing anything...

    how i can i just read the data and right it on the screen
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    103
    Rep Power
    11
    you do something like this

    Code:
    file = open('path\to\file', 'r') # r=read,w=write,a=append,etc
    print file.readlines() #or you could use file.read()
    
    #or you could read it line by line
    print file.readline() #prints first line
    print file.readline() #prints second line
    etc
    It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Not 100% on what your trying to do 7. If your talking about getting user input and outputting to the console window then you looking for the raw_input() function. If not then what are you trying to do with sys.stdin?

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

  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    python prog.py < mytext.txt

    This will read the redirected file:
    Code:
    import sys
    text = sys.stdin.read()
    #Basic printing to stdout
    print text
    #or useing the file methods
    sys.stdout.write(text)
    Grim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by 7imz
    how can i do this operation...

    i've tried

    import sys

    sys.stdout.write(sys.stdin.read())


    but it's not accomplishing anything...

    how i can i just read the data and right it on the screen
    Your code works fine for me:


    Code:
    E:\prj\sandbox>python test.py <test.py
    import sys
    
    sys.stdout.write(sys.stdin.read())
    E:\prj\sandbox>
    Of course if you are taking the input from the keyboard instead of a file or pipe it will keep reading until you enter an EOF character (ctrl-Z on Windows), then it will echo it all in one go. If you want it to echo a line at a time then put it in a loop and use stdin.readline()

    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    57
    Rep Power
    10
    Hi

    I think this is what you rae trying

    import sys
    ans=raw_input(" Enter your name : ")

    print ans
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    2
    Rep Power
    0
    import sys

    print "Enter username:"
    user = sys.stdin.readline().strip()


    [edit]Sorry, we don't allow self promotion until 100 posts and 90 day period.[/edit]
    Last edited by Scorpions4ever; October 13th, 2004 at 11:45 AM.

IMN logo majestic logo threadwatch logo seochat tools logo