473,395 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

subprocess.popen question

I am trying to modify a programming example and I am coming up with
two problems... first is that I can't seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).

import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen(['gawk.exe'],
stdout=subprocess.PIPE).communicate()[0]
xx = 0
for line in last_line:
xx = xx + 1
if xx < 2:
print line
print str(xx)

Jun 20 '07 #1
12 10952
En Wed, 20 Jun 2007 12:27:47 -0300, Er*********@msn.com
<Er*********@msn.comescribió:
I am trying to modify a programming example and I am coming up with
two problems... first is that I can't seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).

import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen(['gawk.exe'],
stdout=subprocess.PIPE).communicate()[0]
You build what appears to be the desired command line, but execute
gawk.exe instead.
Better split the arguments beforehand:
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by line?
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line

--
Gabriel Genellina

Jun 20 '07 #2
On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 20 Jun 2007 12:27:47 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:
I am trying to modify a programming example and I am coming up with
two problems... first is that I can't seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).
import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen(['gawk.exe'],
stdout=subprocess.PIPE).communicate()[0]

You build what appears to be the desired command line, but execute
gawk.exe instead.
Better split the arguments beforehand:
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by line?
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line

--
Gabriel Genellina
I had one test where I had cmd in place of gawk and one everything was
in place of gawk.. I haven't tried what you have listed yet (I am
getting ready to go to work so may have to wait until tommorow to try
it) THanks for the help

Jun 20 '07 #3
On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 20 Jun 2007 12:27:47 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:
I am trying to modify a programming example and I am coming up with
two problems... first is that I can't seem to pass along the
arguments to the external command (I have been able to do that with
the old module and cmd is the command I wish to try) all the output
seems to be returned as one line (at least when I run the program in
spe).
import subprocess
from os import system
cmd = """gawk -f altertime.awk -v time_offset=4 -v
outfile="testdat.sco" "i1.sco" """
#subprocess.Popen.
last_line = subprocess.Popen(['gawk.exe'],
stdout=subprocess.PIPE).communicate()[0]

You build what appears to be the desired command line, but execute
gawk.exe instead.
Better split the arguments beforehand:
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by line?
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line

--
Gabriel Genellina
C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.

I can write it out as a batch file and then run it but that is a messy
hack..

Jun 20 '07 #4
En Wed, 20 Jun 2007 20:02:52 -0300, Er*********@msn.com
<Er*********@msn.comescribió:
On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>>
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by line?
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line

C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.

I can write it out as a batch file and then run it but that is a messy
hack..
If cmd is a list of arguments, like the example above, you should use
subprocess.Popen(cmd,...) (like the example above, too).

--
Gabriel Genellina

Jun 21 '07 #5
On Jun 20, 7:50 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 20 Jun 2007 20:02:52 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:


On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by line?
output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line
C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.
I can write it out as a batch file and then run it but that is a messy
hack..

If cmd is a list of arguments, like the example above, you should use
subprocess.Popen(cmd,...) (like the example above, too).

--
Gabriel Genellina- Hide quoted text -

- Show quoted text -

I had cut and pasted the example in to get that error... could it be
a problem with ms windows??? (I am at a library computer befour work
so that ended my testing)

Jun 21 '07 #6
En Wed, 20 Jun 2007 22:28:06 -0300, Er*********@msn.com
<Er*********@msn.comescribió:
On Jun 20, 7:50 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Wed, 20 Jun 2007 20:02:52 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:
On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by
line?
>output = subprocess.Popen(cmd,
stdout=subprocess.PIPE).communicate()[0]
>lines = output.splitlines()
for line in lines:
print line
C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.
I can write it out as a batch file and then run it but that is a messy
hack..

If cmd is a list of arguments, like the example above, you should use
subprocess.Popen(cmd,...) (like the example above, too).

I had cut and pasted the example in to get that error... could it be
a problem with ms windows??? (I am at a library computer befour work
so that ended my testing)
Note the call to subprocess.Popen - is the first argument [cmd] or cmd?
What do you get with print repr(cmd)?
Do you have gawk installed on that machine too?

--
Gabriel Genellina

Jun 21 '07 #7
On Jun 21, 1:22 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 20 Jun 2007 22:28:06 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:


On Jun 20, 7:50 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Wed, 20 Jun 2007 20:02:52 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:
On Jun 20, 1:46 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
Now, what do you want to do with the output? Printing it line by
line?
output = subprocess.Popen(cmd,
stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line
C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.
I can write it out as a batch file and then run it but that is a messy
hack..
If cmd is a list of arguments, like the example above, you should use
subprocess.Popen(cmd,...) (like the example above, too).
I had cut and pasted the example in to get that error... could it be
a problem with ms windows??? (I am at a library computer befour work
so that ended my testing)

Note the call to subprocess.Popen- is the first argument [cmd] or cmd?
What do you get with print repr(cmd)?
Do you have gawk installed on that machine too?

--
Gabriel Genellina- Hide quoted text -

- Show quoted text -
gawk is installed.. I do fine when I just call gawk I get all the
options to use with it but the minute I try to use the options with it
I have problems. I have done it with batch files but then I would
have to write out a batch file and then run the batch file. seems
like more work than I should have to do to use options with a command
line program.. I have done this in other cases with os.startfile and
other cases and would like to fix it.

Jun 22 '07 #8
En Fri, 22 Jun 2007 01:05:36 -0300, Er*********@msn.com
<Er*********@msn.comescribió:
>cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4", "-v",
"outfile=testdat.sco", "i1.sco"]
output = subprocess.Popen(cmd,
stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line
C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.
I can write it out as a batch file and then run it but that is a
messy
hack..
>If cmd is a list of arguments, like the example above, you should use
subprocess.Popen(cmd,...) (like the example above, too).
I had cut and pasted the example in to get that error... could it be
a problem with ms windows??? (I am at a library computer befour work
so that ended my testing)

Note the call to subprocess.Popen- is the first argument [cmd] or cmd?
What do you get with print repr(cmd)?
Do you have gawk installed on that machine too?

gawk is installed.. I do fine when I just call gawk I get all the
options to use with it but the minute I try to use the options with it
I have problems. I have done it with batch files but then I would
have to write out a batch file and then run the batch file. seems
like more work than I should have to do to use options with a command
line program.. I have done this in other cases with os.startfile and
other cases and would like to fix it.
Ok, but please check *what* are the arguments to Popen. If cmd is a *list*
as shown on the first quoted line on this message, you should call
subprocess.Popen(cmd, ...) as shown on the third line on this message, but
your traceback shows that you are using Popen([cmd], ...)
Can you see the difference?

--
Gabriel Genellina

Jun 22 '07 #9
On Jun 22, 12:10 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 22 Jun 2007 01:05:36 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:


cmd = ["gawk", "-f", "altertime.awk", "-v", "time_offset=4","-v",
"outfile=testdat.sco", "i1.sco"]
output = subprocess.Popen(cmd,
stdout=subprocess.PIPE).communicate()[0]
lines = output.splitlines()
for line in lines:
print line
C:\dex_tracker\pipe1.py
Traceback (most recent call last):
File "C:\dex_tracker\pipe1.py", line 14, in
last_line = subprocess.Popen([cmd],
stdout=subprocess.PIPE).communicate()[0]
File "C:\Python25\lib\subprocess.py", line 593, in __init__
errread, errwrite)
File "C:\Python25\lib\subprocess.py", line 793, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Script terminated.
I can write it out as a batch file and then run it but that is a
messy
hack..
If cmd is a list of arguments, like the example above, you should use
subprocess.Popen(cmd,...) (like the example above, too).
I had cut and pasted the example in to get that error... could it be
a problem with ms windows??? (I am at a library computer befour work
so that ended my testing)
Note the call to subprocess.Popen- is the first argument [cmd] or cmd?
What do you get with print repr(cmd)?
Do you have gawk installed on that machine too?
gawk is installed.. I do fine when I just call gawk I get all the
options to use with it but the minute I try to use the options with it
I have problems. I have done it with batch files but then I would
have to write out a batch file and then run the batch file. seems
like more work than I should have to do to use options with a command
line program.. I have done this in other cases with os.startfile and
other cases and would like to fix it.

Ok, but please check *what* are the arguments toPopen. If cmd is a *list*
as shown on the first quoted line on this message, you should call
subprocess.Popen(cmd, ...) as shown on the third line on this message, but
your traceback shows that you are usingPopen([cmd], ...)
Can you see the difference?

--
Gabriel Genellina- Hide quoted text -

- Show quoted text -
I seemed to have it working sorta when I run it and save the results I
am noticing that in spe it spaces correctly but when I save it to a
file I can open it in wordpad there is only one line. when I open in
up in WinXound (A csound editor) it is double spaced. if I do it in a
batch file the output file is spaced correctly.. when I do splitlines
it is giving me one charecter down the page as output.. Do I need to
do something or can I do something to put an end of line charecter in
the output??

Jun 22 '07 #10
En Fri, 22 Jun 2007 10:08:49 -0300, Er*********@msn.com
<Er*********@msn.comescribió:
I seemed to have it working sorta when I run it and save the results I
am noticing that in spe it spaces correctly but when I save it to a
file I can open it in wordpad there is only one line. when I open in
up in WinXound (A csound editor) it is double spaced. if I do it in a
batch file the output file is spaced correctly.. when I do splitlines
it is giving me one charecter down the page as output.. Do I need to
do something or can I do something to put an end of line charecter in
the output??
Try

print repr(your_data)

to see exactly what you got.

--
Gabriel Genellina

Jun 23 '07 #11
On Jun 23, 5:35 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 22 Jun 2007 10:08:49 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:
I seemed to have it working sorta when I run it and save the results I
am noticing that inspeit spaces correctly but when I save it to a
file I can open it in wordpad there is only one line. when I open in
up in WinXound (A csound editor) it is double spaced. if I do it in a
batch file the output file is spaced correctly.. when I do splitlines
it is giving me one charecter down the page as output.. Do I need to
do something or can I do something to put an end of line charecter in
the output??

Try

print repr(your_data)

to see exactly what you got.

--
Gabriel Genellina
Did you take in account that line endings on Windows are '\r\n' and
not '\n'?

Stani
--
http://pythonide.stani.be

Jun 23 '07 #12
On Jun 23, 6:46 am, SPE - Stani's Python Editor
<spe.stani...@gmail.comwrote:
On Jun 23, 5:35 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:


En Fri, 22 Jun 2007 10:08:49 -0300, Eric_Dex...@msn.com
<Eric_Dex...@msn.comescribió:
I seemed to have it working sorta when I run it and save the results I
am noticing that inspeit spaces correctly but when I save it to a
file I can open it in wordpad there is only one line. when I open in
up in WinXound (Acsoundeditor) it is double spaced. if I do it in a
batch file the output file is spaced correctly.. when I do splitlines
it is giving me one charecter down the page as output.. Do I need to
do something or can I do something to put an end of line charecter in
the output??
Try
print repr(your_data)
to see exactly what you got.
--
Gabriel Genellina

Did you take in account that line endings on Windows are '\r\n' and
not '\n'?

Stani
--http://pythonide.stani.be- Hide quoted text -

- Show quoted text -
evedently when they converted awk from unix to windows (gawk) they
left the formating the same.. python is working corectly then. gawk
seems to return the original line and then the changed line unless I
redirect it to a file.... Thanks for the help, at this point it is up
to me to go through the different versions of awk to find out how they
work with the data... (awke, gawk, mawk, nawk.. exc..)

Jun 25 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: madpython | last post by:
playing with subprocess.Popen on Windows I stumbled into the following problem: Python 2.4.3 (#69, Mar 29 2006, 17:35:34) IDLE 1.1.3 >>> import subprocess >>>...
9
by: Clodoaldo Pinto Neto | last post by:
Output from the shell: $ set | grep IFS IFS=$' \t\n' Output from subprocess.Popen(): "IFS=' \t\n" Both outputs for comparison:
13
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of...
1
by: Mrown | last post by:
Hi, I'm currently writing a python program that relies on a CLI program. What I'm currently doing is using subprocess.Popen on Python 2.5.1. Here's the line that I'm currently running: child...
7
by: skunkwerk | last post by:
Hi, i'm trying to call subprocess.popen on the 'rename' function in linux. When I run the command from the shell, like so: rename -vn 's/\.htm$/\.html/' *.htm it works fine... however when I...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
2
by: dudeja.rajat | last post by:
On Mon, Sep 8, 2008 at 11:50 AM, <dudeja.rajat@gmail.comwrote: Ok, I re-phrase my question: there is a batch file that executes a exe file. The batch just works if run from command prompt and...
1
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've written a class to work with the input and output...
5
by: thedsadude | last post by:
Hello, I'm launching a script as follows: <code> p = subprocess.Popen() p.wait() </code> If p.py writes to sys.stdout, then it is shown on the console.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.