[Tutor] using python to execute from Dir A in Dir B
Andre Walker-Loud
walksloud at gmail.com
Fri Oct 5 04:45:49 CEST 2007
Thank you everyone for the help. I have two solutions, but I would
love one that uses the subprocess.Popen() - I have no experience with
this module.class - if someone with more experience would feel
inclined to provide an example, I would be very much appreciative.
SOLUTION 1 (my brute force method)
create a csh file which does what I want,
##### filename: hack.csh
#!/bin/csh
set RUN_DIR = $1
set EXEC_DIR = $2
cd ${RUN_DIR}
${EXEC_DIR}/my.exe
#####
then I call this in my python code from some arbitrary directory
##### python script...
os.system('./hack.csh /scratch exec_dir')
#####
SOLUTION 2 (courtesy of my computer-superhero friend - again simple)
in my python script...
#####
import os
curdir = os.path.abspath('.') # in case I need to get back to
where I am - in my case no
os.chdir('RUN_DIR') # in my case RUN_DIR = /scratch
if I wanted to get back to my old directory - then add
os.chdir(curdir)
#####
so both of these methods are sort of brute force - being completely
unfamiliar with the subprocess module, again is someone would like to
provide an example, or at least more hints than
'you should use subprocess.Popen()'
I thank you in advance.
Cheers,
Andre
On Oct 4, 2007, at 5:13 PM, Alan Gauld wrote:
> "Andre Walker-Loud" <walksloud at gmail.com> wrote
>
>> If I were using CSH, I could do all this very simply by having these
>> lines in my script
>>
>> ### .csh file
>>
>> cd /scratch
>> my_exe.csh
>
> The best answer is to use subprocess as Kent suggested
> but you can also use os.chdir(path) before using os.system()
> But system() is deprecated in favour of the subprocess module.
>
> Of course you could also modify your script to take a
> path as a command line argument and use that to direct
> the output explicitly...
>
> Alan G.
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list