Piping data into Python on the command line - Windows 2000

David Bolen db3l at fitlinxx.com
Wed Jun 25 17:11:56 EDT 2003


christianzlong at yahoo.com (Christian Long) writes:

> dir | readFromStdin.py        - Doesn't work on win2000
>                              IOError: [Errno 9] Bad file descriptor

I don't know if you're saying you have a problem with Komodo, or just
during your specific tests, but the above case (presuming that you're
running this from a standard Windows command, cmd.exe) is a bug in
Windows cmd.exe.  When trying to use I/O redirection and registered
file extensions to automatically find an executable, the redirection
doesn't work properly.  If you change this command to:

dir | python readFromStdin.py

it should work (replace "python" with the full path to your python.exe
if it isn't in your path).

Whether or not this affects Komodo's Run box depends on just how
Komodo starts up the child process.  It's possible that Komodo is just
executing cmd.exe and passing in its command, in which case supplying
a Python script directly may trigger the same bug as above (or it may
not since it's actually the stdin/stdout to cmd.exe and thus Komodo
that is being passed to the Python script).  If it doesn, then I
expect you'll need to be explicit about Python being the executable
you are running.  Either that or maybe write a wrapper batch file
which can contain the explicit reference to python, to avoid having to
type it into the Komodo Run box.

BTW, another alternative to your script would be:

    import fileinput

    for line in fileinput.input():
        print line,

which will work both with data on stdin as well as having filenames
specified on the command line if desired (also similar to how the Unix
sort command and many other Unix utilities work).  It also doesn't
need to bring all the input into memory before processing (although
you could get that with your prior script by using readline() rather
than readlines()).  The main drawback is that it's not the fastest way
to process lines, but its performance has gotten better in recent
Python releases due to general improvements in file I/O processing.

-- David




More information about the Python-list mailing list