0

I am currently writing a python script that will automate make files that are there in various directories under my project. The directory structure is like this :

                Repository
                  |
                  |
            _____ |_____
           |            |
         Builds        src
           |            |
         __|__        __|__
   Scripts   xyz     abc   3rdpartytools
    |                          |
    conf                     boost
    |                          |
script1.py                     b2

script1.py is my python file and all the make files that I want to execute are present under the 3rdpartytools directory. boost directory is just an example. Now, I want to execute file b2. From the shell, ./b2 works.

I tried using subprocess.call from the python file. I tried

call(["cd"," ../../../src/3rdpartytools/boost;./b2"]) but i get /usr/bin/cd: line 2: cd: ../../../src/3rdpartytools/boost/b2: No such file or directory

The same command when I execute from shell, works fine.

Python 2.7 and CentOS.

5
  • try call(["../../../src/3rdpartytools/boost/b2"]) Commented Sep 26, 2015 at 8:19
  • Tried that out, but I get OSError: [Errno 2] No such file or directory Commented Sep 26, 2015 at 8:25
  • I tried call(["cd","/home/tejas/Repository/src/3rdpartytools/boost;./b2"]) but I still get /usr/bin/cd: line 2: cd: /home/tejas/Repository/src/3rdpartytools/boost/;./b2: No such file or directory.... Works from shell though! Commented Sep 26, 2015 at 8:32
  • Use os.chdir() to change directories before using call(). Commented Sep 26, 2015 at 8:45
  • related: Running a batch file in another directory in python Commented Sep 26, 2015 at 21:34

1 Answer 1

2

You are incorrectly using subprocess.call in multiple ways. subprocess.call will not actually call a shell, unless you specifically ask it to. Thus, the symbol ; has no special meaning. Since it is in the same string as the path, you try to cd into ../../../src/3rdpartytools/boost;./b", not doing a cd and then executing the function.

Use os.chdir to go to the appropriate directory, then call ./b2 using subprocess.call(["./b2"]).

Sign up to request clarification or add additional context in comments.

1 Comment

@Tejas: if you want to change the working directory only for a single command then use call(os.path.join(wdir, 'b2'), cwd=wdir) (no need to call os.chdir())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.