0

Python newbie here.

  1. I created a Python program, msg.py, which outputs a message (Hello, World).

  2. I created a Python program, id.py, which reads input and outputs it.

  3. Using py2exe I created an executable of each of them: msg.exe and id.exe

  4. I created a DOS batch file, set PATH to the dist folder of msg.exe and the dist folder of id.exe.

  5. I then added this to the DOS batch file:

    msg | id

When I ran the batch file I got this error message:

Traceback (most recent call last):
  File "id.py", line 4, in <module>
IndexError: list index out of range

I surmised that the pipe symbol is not feeding the output of msg.exe to the input of id.exe. Is that correct?

  1. So then I placed this in the batch file:

    id < msg

When I ran the batch file I got this error message:

Access is denied.

I really want step 5 to work. That is, I really want to be able to compose programs using the pipe symbol, like so:

A | B | C | ...

Ideally A, B, C, ... could be executables written in various languages, such as Python, C, etc.

How do I get this to work?

Below are the details of what I did. I am running on Windows 7.

Here is msg.py

import stdio

stdio.writeln('Hello, World')

Here is id.py

import sys
import stdio

msg = sys.argv[1]
stdio.writeln(msg)

Here is my DOS batch file:

@echo OFF

set PATH=%PATH%;msg/dist;id/dist

msg | id
Roger Costello
  • 3,007
  • 1
  • 22
  • 43
  • 1
    Your surmising is incorrect. The traceback is telling you that there is an error with `sys.argv[1]`. When you use py2exe and invoke it that way, there is only `sys.argv[0]`. – David K. Hess Jun 20 '15 at 17:39

1 Answers1

3

sys.argv is for command line parameters (id.py a b c).

To read from a pipe, you need to use sys.stdin.read() in id.py.

Edit: See also: https://stackoverflow.com/a/7608205/478656

Community
  • 1
  • 1
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • Thank you very much! Now it works - yay! I am on my way! (Apparently, "msg" is the name of a DOS command, so I had to change from msg.py to mesg.py) – Roger Costello Jun 20 '15 at 18:04