subprocess cwd keyword.

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Thu Oct 26 23:06:19 EDT 2006


Ivan Vinogradov wrote:
> Dear All,
>
> I would greatly appreciate a nudge in the right direction concerning
> the use of cwd argument in the call function from subprocess module.
>
> The setup is as follows:
>
> driver.py		<- python script
> core/  			<- directory
> 	main		<- fortran executable in the core directory
>
>
> driver script generates some input files in the core directory. Main
> should do its thing and dump the output files back into core.
> The problem is, I can't figure out how to do this properly.
>
> call("core/main") works but uses .. of core for input/output.
>
> call("core/main",cwd="core") and call("main",cwd="core") both result in
>    File "driver.py", line 47, in <module>
>      main()
>    File "driver.py", line 40, in main
>      print "OUT", call("core/main", cwd="core")
>    File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
> python2.5/subprocess.py", line 443, in call
>      return Popen(*popenargs, **kwargs).wait()
>    File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
> python2.5/subprocess.py", line 593, in __init__
>      errread, errwrite)
>    File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
> python2.5/subprocess.py", line 1051, in _execute_child
>      raise child_exception
> OSError: [Errno 2] No such file or directory
>
> perhaps if subprocess would indicate the abs path of the object in
> question I could figure it out, but as is I'm lost.


Perhaps you're looking for os.path.abspath?

import subprocess
import os

subdir = os.path.join(*[ os.path.dirname(os.path.abspath(__file__)),
"core" ])
print subdir

try:
    retcode = subprocess.call(["./main"], cwd=subdir)
except:
    raise
 
print retcode

-- 
Hope this helps,
Steven




More information about the Python-list mailing list