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.

Tejas
  • 57
  • 8
  • try `call(["../../../src/3rdpartytools/boost/b2"])` – amdixon Sep 26 '15 at 08:19
  • Tried that out, but I get `OSError: [Errno 2] No such file or directory` – Tejas Sep 26 '15 at 08: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! – Tejas Sep 26 '15 at 08:32
  • Use `os.chdir()` to change directories before using `call()`. – Barmar Sep 26 '15 at 08:45
  • related: [Running a batch file in another directory in python](http://stackoverflow.com/q/32358818/4279) – jfs Sep 26 '15 at 21:34

1 Answers1

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"]).

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 1
    @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()`) – jfs Sep 26 '15 at 21:33