-1

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

jps
  • 20,041
  • 15
  • 75
  • 79
  • [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Jun 05 '22 at 18:19
  • 1
    If `cwd="C:\iot_flasher"` is incorrect, where _do_ you have the file? (Notice also how the lone backslash is wrong; you want `cwd=r"C:\iot_flasher"` or `cwd="C:\\iot_flasher"`) – tripleee Jun 05 '22 at 18:21
  • As an aside, prefer `subprocess.run` over `Popen` unless you specifically need the fine-grained detailed control of a parallel process and the responsibilities of managing it for the remainder of its lifetime. (Also, probably [avoid `shell=True`](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess)) – tripleee Jun 05 '22 at 18:23
  • Hi Tripleee, thanks for the reply. I have the .hex file in "c:\iot_flasher" I have changed it to "c:\\iot_flasher" with same result: ERROR: THe file specified could not be found – gusfragger Jun 05 '22 at 18:28
  • It is working now! Regarding prefering subprocess.run over Popen, can I catch the console output to display it in the text box as I do in my code? – gusfragger Jun 05 '22 at 18:34
  • Absolutely, just capture it with `capture_output=True, text=True`. See also https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538#51950538 – tripleee Jun 05 '22 at 18:46

1 Answers1

0

Based on what I understood from your question (maybe reword it because it's a little confusing) instead of just writing command = "nrfjprog --program slim.hex --chiperase --reset" write command = "nrfjprog --program C:\ExampleDirectory\wherever\slim.hex --chiperase --reset"

Like I said, I may be wrong, but that's what I think should work.

Craze XD
  • 110
  • 8
  • 1
    Sorry if it was confusing, but yes, your answer was accurate to my question, it is working now , thanks! – gusfragger Jun 05 '22 at 18:36
  • @gusfragger is it working now because you followed this answer? If so, you should accept it. Was this answer helpful to you? If so, consider upvoting it. – Neil Jun 05 '22 at 18:50
  • I can't upvote yet because I need 15 reputation, I've just created my account yesterday and I'm trying to get familiar with how everything works here – gusfragger Jun 06 '22 at 19:42