[Tutor] invoking system commands from python

Fulvio Copex copellifulvio at yahoo.it
Fri Oct 1 14:59:42 CEST 2004


Dear Danny,
thank you very much for your kind answer.
Besides solving properly the first part of the problem, you undirectly gave me some hints.
The program, modified as you suggested, works properly.
The fact is that the function copy was an example just to make things easier to understand.
The command I want to launch is "R language" a powerful statistical application, the parameters are built-in functions of R language.
Maybe this is too difficult to do just with the python function 'popen' .
In fact I tried:
 
****************************************
import os
os.chdir('C:\\Programmi\\R\\rw1090\\miei_files')
cmd = '.RData --slave --no -save'
parameters="q()"
myCmd=os.popen("%s %s" % (cmd, parameters))
exitCode = myCmd.close()
if exitCode:
    print '%s failed, this probably says why:\n%s' % (cmd, stdout)
****************************************
and the R language gui application opens properly, but the q() function (that should close the gui) doesn't seem to be invoked.
How can I get the system reply when I launch a command?
 
However as you suggested, maybe it's better to look if someone has already built the wheel, and in fact I found that there are two python packages: 
Rpy and RSPython.
that deal with R language
I will try to make use of them.
I wonder, have you never heard about them?
 
Thanks again, 
cheers,
Fulvio.

Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:


On Wed, 29 Sep 2004, Fulvio Copex wrote:

> i'm new to this list and to python too.
> I'm using active state python 2.3 on win32,
> my problem is the following:
> I want learn how to execute system command passing parameters.
>
> for example to copy a file I've tried to write this script:
>
> *****************************
> import os
> os.chdir(''myDirectory")
> cmd="copy"
> parameters="file1 file1_backup"
> myCmd = os.popen(cmd,'w')
> myCmd.write(parameters)
> exitCode = myCmd.close()
> if exitCode:
> print '%s failed, this probably says why:\n%s' % (cmd, myCmd)


Hi Fulvio,


There's a syntax problem here:

> os.chdir(''myDirectory")
^^

but you probably have fixed this already. There's a more important
problem in the use of os.popen():

> myCmd = os.popen(cmd,'w')
> myCmd.write(parameters)


Your 'copy' command may need to take in the parameters at the same time as
the popen call, as additional "command-line arguments". I'm not sure how
your 'copy' command works, but it may not be listening to standard input.

So you may need to do something like this:

os.popen("%s %s" % (cmd, parameters))

where we construct the command line all at once, and then pass that off to
popen().


That being said, when you're programming in Python, you actually want to
avoid using os.popen() or os.system() if you can help it. *grin*

The reason is because they're really OS-specific: you can't then take the
same program on Windows and expect it to work on a Unix system, at least,
not without a lot of work. It's also fraught with security problems.



There's already a file-copying function in the 'shutil' shell utilities
module in the Standard Library:

http://www.python.org/doc/lib/module-shutil.html


So a better way to do file copying is to use shutils.copy().

###
import os
import shutils
os.chdir("myDirectory")
shutils.copy("file1", "file1_backup")
###

Not only is this easier to read, but it should be much more portable
across operating systems.


If you have more questions, please feel free to ask. Good luck!


				
---------------------------------
Scopri Mister Yahoo! -  il fantatorneo sul calcio di Yahoo! Sport'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20041001/b0d1df2f/attachment.html


More information about the Tutor mailing list