Redirecting output in Windows NT

Bengt Richter bokr at oz.net
Tue Mar 12 12:43:27 EST 2002


On Tue, 12 Mar 2002 09:03:12 GMT, Mark Hammond <mhammond at skippinet.com.au> wrote:

>Paul Lydon wrote:
>> Running Python 2.2 on Windows NT 4 Workstation.
>> Suppose I have a Python program named 'splat.py' consisting of:
>> "print 'splat'".
>> 
>> If I type 'splat' and hit 'Enter', NT knows that it must run the
>> Python interpretor and I see 'splat' on the screen. If I redirect the
>> output to a file:
>> 'splat > splat.txt', the resulting file is 0 bytes long and empty.
>>
The same thing would happen with Perl, if you have that installed
and type 'splat.pl > splat.txt' (or 'splat > splat.txt' if .PL has
priority over .PY in PATHEXT ;-)

Anyway, it's a windows wart you can usually work around by invoking
the interpreter (or other program) explicitly rather than through
file extension association, e.g.,

    python pyscript.py script_arg1 script_arg2 > captured_stdout

If you run your script a lot, you may find it convenient to wrap it in
a .cmd file. E.g., I collect a lot of little utilities in c:\util,
and just added pargs.py and its wrapper pypargs.cmd as follows,
simulating your situation (although apparently you've set .PY in
PATHEXT or you'd have had to type 'splat.py > splat.txt' as I did below.):
(dirf is a wrapped perl thing from long ago to munge dir output
into something I like better).
--
[ 9:34] C:\pywk>type \util\pargs.py
import sys
for i in xrange(len(sys.argv)):
    print 'argv[%d]: "%s"' % (i,sys.argv[i])
[ 9:34] C:\pywk>type \util\pypargs.cmd
@d:\python22\python c:\util\pargs.py %*
[ 9:35] C:\pywk>pargs.py 1 "two with spaces" 3
argv[0]: "C:\UTIL\pargs.py"
argv[1]: "1"
argv[2]: "two with spaces"
argv[3]: "3"

[ 9:35] C:\pywk>pargs.py 1 "two with spaces" 3 > junk.txt

[ 9:35] C:\pywk>dirf junk.txt
02-03-12  09:35          0 C:\pywk\junk.txt

[ 9:35] C:\pywk>pypargs 1 "two with spaces" 3
argv[0]: "c:\util\pargs.py"
argv[1]: "1"
argv[2]: "two with spaces"
argv[3]: "3"

[ 9:36] C:\pywk>pypargs 1 "two with spaces" 3 > junk.txt

[ 9:36] C:\pywk>dirf junk.txt
02-03-12  09:36         85 C:\pywk\junk.txt

[ 9:36] C:\pywk>type junk.txt
argv[0]: "c:\util\pargs.py"
argv[1]: "1"
argv[2]: "two with spaces"
argv[3]: "3"
--

>> If I run the same program directly by typing 'python splat.py', again
>> I see 'splat' on the screen and when the output is redirected 'python
>> splat.py > splat.txt' I get a file containing the string 'splat' (as
>> you would expect).
>> 
>> Any ideas why the output redirection doesn't work in the first place?
>
>It is a bug in the NT command prompt.  Works OK in Win2k.  I believe 
>there is a FAQ on it too.
>
IIRC you can get the analogous problem in other ways than just interactively
if you rely on file-association-driven program invocation.

Regards,
Bengt Richter




More information about the Python-list mailing list