Some background: I have been coding for a while now as a hobbie, mostly javascript. At my work, we do some mechanics and eventually we have to flash some boards, process that requiere running a few commands in the command line. For some of us, this is okay, but for most of the other mechanics this seems hard to do, so I proposed that I will build an interface where they can just choose the model and the operation, press a button and the commands will run in the background (easier say than done). I have chosen python to do this, and I'm learning a lot in the process, luckily tackling a lot of the issues. But here is one I can't get around: One of the commands, executes a .hex file with firmware, but since the .py file is executing from where python is installed basically, (and not where this .hex file is) the execution fails and says it cant find the file Here is my code:
def run_command(command):
return Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=True,
cwd="C:\iot_flasher")
def btn_click():
output_box.delete("1.0","end")
if(iot_model.get() == "V3x Catacomb"):
if (mode_selection.get() == 0):
command = "nrfjprog -d"
elif (mode_selection.get() == 1):
command = "nrfjprog --eraseall"
elif (mode_selection.get() == 2):
command = "nrfjprog --program slim.hex --chiperase --reset"
with run_command(command) as p:
if p.stdout:
for line in p.stdout:
output_box.insert(END, line)
output_box.configure(background="green")
if p.stderr:
for line in p.stderr:
output_box.insert(END, line)
output_box.configure(background="red")
I have ommited the tkinter part, but the interface looks like this: enter image description here
and I add some other pics to show how it works when the command execution is correct or when it fails, wanted to make it really visual. enter image description here enter image description here
After reading the subprocess documentation, I have tried to determine the folder where I want the script to execute from with cwd="C:\iot_flasher" , but this didn't do the trick.
Really looking forward for some help from someone with experience in python