RE: Some general questions about using stdin,stdout....
thank you very much for your help... my big mistake,was to believe that | is the pipe symbol for both,unix and python... it is really annoying,how such a simple thing can mess things Thank you for clearing this out. _ Free blogging with MSN Spaces http://spaces.msn.com/?mkt=nl-be -- http://mail.python.org/mailman/listinfo/python-list
Re: Some general questions about using stdin,stdout....
asdsd sir wrote: thank you very much for your help... my big mistake,was to believe that | is the pipe symbol for both,unix and python... it is really annoying,how such a simple thing can mess things Thank you for clearing this out. It is indeed annoying when assumptions we carry from one environment turn out to be untrue for another. I can almost hear the unspoken thought (But Perl is similar to shell scripting in this way, so why aren't other languages?) - though I may be doing you a disservice assuming that you know anything about Perl. You will find as your programming experience increases that the different languages you learn are appropriate for different purposes, and have different advantages and disadvantages. Python excels at expressing algorithms in an unambiguous and easily-readable way. It's worth taking the time to learn it, and you are clearly already on your way. I look forward to reading your next question. Welcome to comp.lang.python. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/ -- http://mail.python.org/mailman/listinfo/python-list
Some general questions about using stdin,stdout....
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat file.txt|python somefile.py #somefile.py import sys text=sys.stdin.read() ...then sys.stdin.read() will read from cats stdout... However,if i type inside a program,something like #someprog.py import sys print hello|sys.stdin.read() .the screen hangs..why is that?isn't the same situation as cat? in addition to this... Why can't i write to the stdin? Isn't it being used as a file object like all the others? for example sys.stdin.close() or open('sys.stdin','w+') or sys.stdin.write(something) etc... don't work... At last,let's consider sys.stdin.read(3) If i type that,the program waits for me to type some characters,and then prints the first three... However,doesn't this action actually include a writing to stdin and then a reading from that? I'm really confused... Any help would be highly appreciated... Thanks a lot. _ Free blogging with MSN Spaces http://spaces.msn.com/?mkt=nl-be -- http://mail.python.org/mailman/listinfo/python-list
Re: Some general questions about using stdin,stdout....
Hello. If you're new to Python, then input/output isn't the best place to start. Begin with the tutorial: http://docs.python.org/tut/tut.html Other documentation is also linked to from there. However, I will briefly answer your questions. print hello|sys.stdin.read() In Python the | operator has a different meaning than in a shell. In Python it means bitwise or: print 5 | 9 13 Why can't i write to the stdin? There may be some contorted way to do that under Unix, but it's not a good idea. You can pipe input to your program from a shell (as you've already done), but it's not a good idea to redirect input from within your program. You can accomplish the same thing like this: if i_want_to_redirect_input: my_input_source = open('file.txt') else: my_input_source = sys.stdin # Now read from my_input_source instead of stdin. -- They had a big meeting, drank some beer and had some pizza and decided 'A' would be 65. - Jim Greenly, professor at Georgia Institute of Technology -- http://mail.python.org/mailman/listinfo/python-list
Re: Some general questions about using stdin,stdout....
asdsd sir wrote: Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat file.txt|python somefile.py #somefile.py import sys text=sys.stdin.read() ...then sys.stdin.read() will read from cats stdout... However,if i type inside a program,something like #someprog.py import sys print hello|sys.stdin.read() .the screen hangs..why is that?isn't the same situation as cat? Obviously not... The stdin is a read-only file that you can read from the data that is passed via a pipe to your application. You did tha piping by using cat file.txt | python somefile.py That establihes the pipe between cat's stdout(!) and python's stdin. That's the reason that | is called pipe or pipe-operator in the SHELL(!) print hello|sys.stdin.read() OTH is inside python, and | is not the pipe-operator, but the binary or-operator. Consider this: print 1 | 2 3 But: print hello | some other string Traceback (most recent call last): File stdin, line 1, in ? TypeError: unsupported operand type(s) for |: 'str' and 'str' So you don't write something to stdin by that. Instead it waits endlessly, trying to read something that is piped to it from the outside. But if that was the case, it would puke on you with the above error message. in addition to this... Why can't i write to the stdin? Isn't it being used as a file object like all the others? for example sys.stdin.close() or open('sys.stdin','w+') or sys.stdin.write(something) etc... don't work... Because it is defined that way. I suggest you read up on unix piping to grasp the concepts behind it - python only follows these. Diez -- http://mail.python.org/mailman/listinfo/python-list