R BATCH jobs from Python

Benjamin Scott benjamin.scott at gmail.com
Sun Nov 21 14:02:28 EST 2004


Terry Hancock <hancock at anansispaceworks.com> wrote in message news:<mailman.6654.1100995055.5135.python-list at python.org>...
> On Saturday 20 November 2004 05:30 pm, Benjamin Scott wrote:
> > Author: Moosebumps
> > Subject (1): Batch commands on Windows
> > Subject (2): os.system always opens new window on Windows XP/2000
> > 
> > here is the operation that works, but isn't actually carried out in
> > Python:
> > 
> > Start->Run "cmd" ***command line appears***
> > 
> > then i enter the following commands (i copied and pasted the whole
> > session so that you can see exactly what i did):
> > ******************************************************
> > Microsoft Windows XP [Version 5.1.2600]
> > (C) Copyright 1985-2001 Microsoft Corp.
> > 
> > C:\Documents and Settings\BRdir>cd c:\Applications\R\rw1091\bin
> > 
> > C:\Applications\R\rw1091\bin>Rcmd BATCH c:\test.txt
> > 
> > C:\Applications\R\rw1091\bin>
> > ******************************************************
> 
> It would have been better to show us the Python code that *didn't*
> work. ;-)

i was trying to spare you the pain... ;)

> 
> But I suspect it looks like this:
> 
> os.system('cd c:\Applications\R\rw1091\bin')
> os.system('Rcmd BATCH c:\test.txt')

yes, in fact it did look something like this.

> 
> and your complaint is that the first 'cd' doesn't affect
> the second command's context (i.e. they are in separate
> shells).
> 

yes, i couldn't pass multiple commands.  that was the main problem. 
the shell would immediately close once it was opened.

> You are right that os.system() uses separate shells, and
> this is what it's supposed to do.
> 
> There are several fixes for this particular problem. One
> is to do the 'cd' in python:
> 
> os.chdir('c:\Applications\R\rw1091\bin')
> os.system('Rcmd BATCH c:\test.txt')
> 

this worked... i think i changed the backslashes to double
backslashes, but with minor alterations it worked.  thanks very much.

> Another is to just run the command directly:
> 
> os.system('c:\Applications\R\rw1091\bin\Rcmd BATCH c:\test.txt')
> 
> Either should work fine for your example.
> 
> And, of course, it should be mentioned that there exists
> a set of R bindings for Python so you could drive R directly,
> instead of kludging a script driven from a system call in
> Python.
> 

yes, the following poster, Tim C, mentions RPy.  i have tinkered with
RPy and with RSPython from omegahat.  RPy seems to work as you would
want/expect.  just takes a bit of time to get used to the syntax.  i
think this might be overkill in this case unless there are major
performance differences of which i am unaware.

> You can also do it with popen, but it's probably less
> intuitive than the solutions above (in general, popen isn't
> really worth it unless you want to pipe the stdin/stdout
> through your program -- which you don't seem to need here?).

this is the type of stuff that adds unnecessary confusion for
developers attempting to use new tools.  that is, there seems to be
multiple tools that all have overlapping functionality, but they are
administered differently.  this was part of the motivation for writing
popen5 (renamed: subprocess), but in order for this tool to be
accessible to new developers it needs more examples in its
documentation... or there needs to be a crossreference with more
examples existing elsewhere.  popen5 has many examples on how the new
tool is related to the handful of tools that came before it, but this
isn't particularly helpful to a newcomer to the tool.

tools that allow interprocess/application communication seem like they
add so much value to the parent application; i am surprised i wasn't
able to find some kind of beginner's "Quick Start" online somewhere. 
anyhow, that's my $.02.

> 
> Cheers,
> Terry

thanks again for your feedback,

benjamin scott



More information about the Python-list mailing list