[Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread George R Goffe
Hi,

When you run a python program, it appears that stdin, stdout, and stderr are 
opened automatically.

I've been trying to find out how you tell if there's data in stdin (like when 
you pipe data to a python program) rather 
than in a named input file. It seems like most/all the Unix/Linux 
commands are able to figure this out. Do you know how Python programs do this 
or might do this?

MANY thanks for any/all help/hints/tips/suggestions,

George...
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread Ben Finney
George R Goffe  writes:

> When you run a python program, it appears that stdin, stdout, and
> stderr are opened automatically.

That's true of any program on a POSIX-compliant operating system.

> I've been trying to find out how you tell if there's data in stdin
> (like when you pipe data to a python program) rather than in a named
> input file.

What does “there's data in [a stream]” mean here, and how is it distinct
from there being “data in … a named input file”?

The advantage of the standard POSIX streams is that processes can treat
them as very nearly normal files. I don't doubt you have a distinction
you want to detect, but can you be clearer about what that distinction
is?

> It seems like most/all the Unix/Linux commands are able to figure this
> out. Do you know how Python programs do this or might do this?

Hmm. The standard input stream is a separate object from any named input
file you might otherwise open. That's a trivially obvious difference to
detect, so I guess you must not mean that.

Perhaps you mean “is the ‘stdin’ stream connected to an interactive
terminal?”. That's quite a different question from what you seem to be
asking, so I don't know.

-- 
 \   “The best is the enemy of the good.” —Voltaire, _Dictionnaire |
  `\Philosophique_ |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread wolfrage8...@gmail.com
Are you planning to pipe data to a python program? If so please
specify and you will get more complete answers.
Specifically I am thinking you want information pertaining to
subprocess in the standard library.
https://docs.python.org/3/library/subprocess.html

On Sat, Oct 18, 2014 at 2:36 PM, George R Goffe
 wrote:
> Hi,
>
> When you run a python program, it appears that stdin, stdout, and stderr are 
> opened automatically.
>
> I've been trying to find out how you tell if there's data in stdin (like when 
> you pipe data to a python program) rather
> than in a named input file. It seems like most/all the Unix/Linux
> commands are able to figure this out. Do you know how Python programs do this 
> or might do this?
>
> MANY thanks for any/all help/hints/tips/suggestions,
>
> George...
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A question about using stdin/out/err vs named files

2014-10-18 Thread Steven D'Aprano
On Sat, Oct 18, 2014 at 11:36:43AM -0700, George R Goffe wrote:
> Hi,
> 
> When you run a python program, it appears that stdin, stdout, and 
> stderr are opened automatically.
> 
> I've been trying to find out how you tell if there's data in stdin 
> (like when you pipe data to a python program) rather than in a named 
> input file. It seems like most/all the Unix/Linux commands are able to 
> figure this out. Do you know how Python programs do this or might do 
> this?

Hmmm, good question. I've never actually done this before, but I think 
the right way is to just try reading from stdin and see if anything is 
there. Let's try it, using Python 2.7:

# stdin_demo.py
import sys
c = sys.stdin.read(1)
if c:
text = c + sys.stdin.read()  # Slurp everything.
print "Read text from stdin:"
print text
else:
print "Nothing in stdin."



Alas, that doesn't do what I expected. It works if I pipe something 
into stdin:

[steve@ando ~]$ echo "Some data here" | python2.7 stdin_demo.py
Read text from stdin:
Some data here



but if stdin is empty, the call to read() blocks, waiting for input. I 
have to manually type something (or nothing, as the case may be) 
then type Ctrl-D to force end-of-file:

[steve@ando ~]$ python2.7 stdin_demo.py  # enter something, then Ctrl-D
hello world
Read text from stdin:
hello world

[steve@ando ~]$ python2.7 stdin_demo.py  # immediately Ctrl-D
Nothing in stdin.



Here's my second attempt:

# stdin_demo.py
import sys
if sys.stdin.isatty():
print "Ignoring stdin."
else:
c = sys.stdin.read(1)
if c:
text = c + sys.stdin.read()  # Slurp everything.
print "Read text from stdin:"
print text
else:
print "Nothing in stdin."



This version seems to do what I think you want:


[steve@ando ~]$ python2.7 stdin_demo.py
Ignoring stdin.
[steve@ando ~]$ echo "Some data here" | python2.7 stdin_demo.py
Read text from stdin:
Some data here

[steve@ando ~]$ echo -n "" | python2.7 stdin_demo.py
Nothing in stdin.


I haven't tried this under Windows, and I'm not sure if it is the 
"correct" way to do it under POSIX systems like Unix, Linux or Mac, but 
it seems to work.

Oh, in case it wasn't obvious... it's probably not a good idea to slurb 
everything as I do above, but I leave processing stdin line-by-line as 
an exercise.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor