ftkhateeb
ftkhateeb

Reputation: 29

How to make my python function go to a specific folder path and run a command line

I am trying to automate my build process. typically what I do is I go to a certain folder where my make scripts are at, open a CMD, type a command which is something like this "Bootstrap_make.bat /fic = Foo"

Now I want to do this from inside my python code. simple question is How to do this, i.e. how to; using python code. go to a certain path > and open a CMD > and execute the command"

here is what I've tried

def myClick():
subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ])
subprocess.run(["bootstrap_make.bat","/fic= my_software_variant"])

However I git this error :

Traceback (most recent call last): File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call return self.func(*args) File "C:\E\ProductOne_Builder1.py", line 5, in myClick subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ]) File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in init self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified

Upvotes: 0

Views: 2190

Answers (2)

user19707153
user19707153

Reputation:

You could use os.system(). https://docs.python.org/3/library/os.html

This code is assuming you are on Windows because you are running a .bat file and trying to open a CMD window.

import os

directory = './path/to/make/scripts'
buildCommand = './Bootstrap_make.bat'
buildArgs = '/fic = Foo'
# if you don't want a new cmd window you could do something like:
os.system('cd {} && {} {}'.format(directory, buildCommand, buildArgs))

# if you want a new cmd window you could do something like:
os.system('cd {} && start "" "{}" {}'.format(directory, buildCommand, buildArgs))

Upvotes: 1

iHowell
iHowell

Reputation: 2447

You can use subprocess.run, specifying the cwd (current working directory) kwarg.

Upvotes: 0

Related Questions