Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Run a external program.

3 views
Skip to first unread message

Yasser Almeida Hernández

unread,
Nov 14, 2009, 2:26:11 PM11/14/09
to pytho...@python.org
Hi all!!

I'm writing a script where i call a external program which receive
some arguments.
One of this arguments is stored in a variable, that is passed as
argument as well:

import os
...
f = open(file1, 'r')
s = 'command $f -i file2 -w 1.4 -o file3.out'
os.system(s)
...

When i run the script i get the next message...
'-i: No such file or directory'
... with a obvious error in the exit of the program. If i remove the
option -i i get the same error with every option, even with those who
don't get any file as argument. (file2 exist).
BUT, when i run the external program in a python shell, it works...

What's wrong?

Please help me...
Thanks


--
Lic. Yasser Almeida Hernández
Center of Molecular Inmunology (CIM)
Nanobiology Group
P.O.Box 16040, Havana, Cuba
Phone: (537) 271-7933, ext. 221

----------------------------------------------------------------
Correo FENHI

MRAB

unread,
Nov 14, 2009, 2:53:50 PM11/14/09
to pytho...@python.org
Yasser Almeida Hern�ndez wrote:
> Hi all!!
>
> I'm writing a script where i call a external program which receive some
> arguments.
> One of this arguments is stored in a variable, that is passed as
> argument as well:
>
> import os
> ...
> f = open(file1, 'r')
> s = 'command $f -i file2 -w 1.4 -o file3.out'
> os.system(s)
> ...
>
> When i run the script i get the next message...
> '-i: No such file or directory'
> ... with a obvious error in the exit of the program. If i remove the
> option -i i get the same error with every option, even with those who
> don't get any file as argument. (file2 exist).
> BUT, when i run the external program in a python shell, it works...
>
> What's wrong?
>
The name 'f' in the Python script exists only in Python and is unrelated
to the '$f' that the shell sees.

Mark Tolonen

unread,
Nov 14, 2009, 3:03:23 PM11/14/09
to pytho...@python.org

"Yasser Almeida Hern�ndez" <pedr...@fenhi.uh.cu> wrote in message
news:20091114142611....@correo.fenhi.uh.cu...

> Hi all!!
>
> I'm writing a script where i call a external program which receive some
> arguments.
> One of this arguments is stored in a variable, that is passed as argument
> as well:
>
> import os
> ...
> f = open(file1, 'r')
> s = 'command $f -i file2 -w 1.4 -o file3.out'
> os.system(s)
> ...
>
> When i run the script i get the next message...
> '-i: No such file or directory'
> ... with a obvious error in the exit of the program. If i remove the
> option -i i get the same error with every option, even with those who
> don't get any file as argument. (file2 exist).
> BUT, when i run the external program in a python shell, it works...
>
> What's wrong?

Please post a small, complete example of your code and the error message.
Cut-and-paste them exactly. Also provide the shell command you are running
that works.

-Mark


Yasser Almeida Hernández

unread,
Nov 14, 2009, 3:07:26 PM11/14/09
to MRAB, pytho...@python.org
So, how can i pass an argument as a variable in this context...?


Quoting MRAB <pyt...@mrabarnett.plus.com>:

> Yasser Almeida Hernández wrote:
>> Hi all!!
>>
>> I'm writing a script where i call a external program which receive
>> some arguments.
>> One of this arguments is stored in a variable, that is passed as
>> argument as well:
>>
>> import os
>> ...
>> f = open(file1, 'r')
>> s = 'command $f -i file2 -w 1.4 -o file3.out'
>> os.system(s)
>> ...
>>
>> When i run the script i get the next message...
>> '-i: No such file or directory'
>> ... with a obvious error in the exit of the program. If i remove
>> the option -i i get the same error with every option, even with
>> those who don't get any file as argument. (file2 exist).
>> BUT, when i run the external program in a python shell, it works...
>>
>> What's wrong?
>>

> The name 'f' in the Python script exists only in Python and is unrelated
> to the '$f' that the shell sees.

> --
> http://mail.python.org/mailman/listinfo/python-list

Chris Rebert

unread,
Nov 14, 2009, 3:50:14 PM11/14/09
to alm...@cim.sld.cu, pytho...@python.org
> Quoting MRAB <pyt...@mrabarnett.plus.com>:
>> Yasser Almeida Hernández wrote:
>>>
>>> Hi all!!
>>>
>>> I'm writing a script where i call a external program which receive  some
>>> arguments.
>>> One of this arguments is stored in a variable, that is passed as
>>>  argument as well:
>>>
>>> import os
>>> ...
>>> f = open(file1, 'r')
>>> s = 'command $f -i file2 -w 1.4 -o file3.out'
>>> os.system(s)
>>> ...
>>>
>>> When i run the script i get the next message...
>>> '-i: No such file or directory'
>>> ... with a obvious error in the exit of the program. If i remove  the
>>> option -i i get the same error with every option, even with  those who don't
>>> get any file as argument. (file2 exist).
>>> BUT, when i run the external program in a python shell, it works...
>>>
>>> What's wrong?
>>>
>> The name 'f' in the Python script exists only in Python and is unrelated
>> to the '$f' that the shell sees.

2009/11/14 Yasser Almeida Hernández <pedr...@fenhi.uh.cu>:


> So, how can i pass an argument as a variable in this context...?

Use the string variable's value when specifying the arguments to the command.

Here's how you'd do it using the newer `subprocess` module:

import sys
import subprocess
args = ['command', file1, '-i', 'file2', '-w', '1.4', '-o',
'file3.out'] #assuming only file1 is variable
return_code = subprocess.call(args, stdin=sys.stdin,
stdout=sys.stdout, stderr=sys.stderr)

Cheers,
Chris
--
http://blog.rebertia.com

MRAB

unread,
Nov 14, 2009, 3:56:30 PM11/14/09
to pytho...@python.org
Yasser Almeida Hern�ndez wrote:
> So, how can i pass an argument as a variable in this context...?
>
You can't pass arbitrary values on a command line. In this case, why not
just pass the path of the file?

s = 'command "%s" -i file2 -w 1.4 -o file3.out' % file1

>
> Quoting MRAB <pyt...@mrabarnett.plus.com>:

Yasser Almeida Hernández

unread,
Nov 14, 2009, 4:23:22 PM11/14/09
to MRAB, pytho...@python.org
All ran ok!!

Thanks a lot

Quoting MRAB <pyt...@mrabarnett.plus.com>:

> Yasser Almeida Hernández wrote:
>> So, how can i pass an argument as a variable in this context...?
>>
> You can't pass arbitrary values on a command line. In this case, why not
> just pass the path of the file?
>
> s = 'command "%s" -i file2 -w 1.4 -o file3.out' % file1
>
>>
>> Quoting MRAB <pyt...@mrabarnett.plus.com>:
>>

>>> Yasser Almeida Hernández wrote:
>>>> Hi all!!
>>>>
>>>> I'm writing a script where i call a external program which
>>>> receive some arguments.
>>>> One of this arguments is stored in a variable, that is passed as
>>>> argument as well:
>>>>
>>>> import os
>>>> ...
>>>> f = open(file1, 'r')
>>>> s = 'command $f -i file2 -w 1.4 -o file3.out'
>>>> os.system(s)
>>>> ...
>>>>
>>>> When i run the script i get the next message...
>>>> '-i: No such file or directory'
>>>> ... with a obvious error in the exit of the program. If i remove
>>>> the option -i i get the same error with every option, even with
>>>> those who don't get any file as argument. (file2 exist).
>>>> BUT, when i run the external program in a python shell, it works...
>>>>
>>>> What's wrong?
>>>>
>>> The name 'f' in the Python script exists only in Python and is unrelated
>>> to the '$f' that the shell sees.
>>
>

Terry Reedy

unread,
Nov 14, 2009, 8:14:23 PM11/14/09
to pytho...@python.org
Top-posting makes things more confusing. You cannot pass a Python file
object to an external process. Pass the name instead.


Yasser Almeida Hernández wrote:
> So, how can i pass an argument as a variable in this context...?
>
>

0 new messages