subprocess cwd keyword.


I

Ivan Vinogradov

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.
 
Ad

Advertisements

A

attn.steven.kuo

Ivan said:
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
 
L

Leo Kislov

Ivan said:
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
[snip exception]

Usually current directory is not in the PATH on UNIX. Try
call("./main",cwd="core")

-- Leo
 
Ad

Advertisements

I

Ivan Vinogradov

Ivan said:
...

call("core/main") works but uses .. of core for input/output.

call("core/main",cwd="core") and call("main",cwd="core") both
result in
[snip exception]

Usually current directory is not in the PATH on UNIX. Try
call("./main",cwd="core")

-- Leo

Thank you both Leo and Steven.
The solution was indeed calling "main" as "./main" once cwd was changed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top