I noticed the following using Python 2.5.2 (does not occur using 2.7):
#!/usr/bin/python
import sys
for line in sys.stdin:
print line,
Output:
$ echo -e "one\ntwo\nthree" | python test.py
$ one
$ two
$ three
as expected. However, if I import subprocess
to the this script:
#!/usr/bin/python
import sys
import subprocess
for line in sys.stdin:
print line,
Output:
$ echo -e "one\ntwo\nthree" | python test.py
$ two
$ three
What happened to the first line of output?
Update:
I think I may have discovered the root of the problem. I had a file named time.py
in my cwd
. A time.pyc
is being created every time I run the script with subprocess
imported, suggesting that ./time.py
is also being imported. The script runs normally if I delete the .pyc
and time.py
files; however, there is still the question of why a subprocess
import would cause ./time.py
to be imported as well?
I have narrowed it down even further to the exact line in time.py
that causes the strange behaviour. I have stripped down the working dir and file content to just that which affects the output:
test.py
#!/usr/bin/python
import sys
import subprocess
for line in sys.stdin:
print line,
time.py
#!/usr/bin/python
import sys
for line in sys.stdin:
hour = re.search(r'\b([0-9]{2}):', line).group(1)
Running test.py
with any kind of input results in the first line of output being omitted and time.pyc
being created.
time.py
file do any input when run? If so, it's probably eating your "one" line. It's generally a bad idea to name your own modules with the same name as something in the standard library for this reason! Packages can help a bit. – Pulsatorytime.py
doesn't run; it's not a valid script as it is. – Tamiwait
. Anyway. This honestly is not the question that deserves time spent on it or upvotes it received. – Gangboard