title: How to run a shell command using python from a specific location?
body: <p>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:</p>
  <pre><code>def run_command(command):
      return Popen(command, 
                  stdout=subprocess.PIPE,
                  stderr=subprocess.PIPE,
                  universal_newlines=True,
                  shell=True,
                  cwd=&quot;C:\iot_flasher&quot;)
  
  
  
  
  def btn_click():
      output_box.delete(&quot;1.0&quot;,&quot;end&quot;)
      if(iot_model.get() == &quot;V3x Catacomb&quot;):
          if (mode_selection.get() == 0):
              command = &quot;nrfjprog -d&quot;
          elif (mode_selection.get() == 1):
              command = &quot;nrfjprog --eraseall&quot;
          elif (mode_selection.get() == 2):
              command = &quot;nrfjprog --program slim.hex --chiperase --reset&quot;
          with run_command(command) as p:
              if p.stdout:
                  for line in p.stdout:
                      output_box.insert(END, line)
                      output_box.configure(background=&quot;green&quot;)
              if p.stderr:
                  for line in p.stderr:
                      output_box.insert(END, line)
                      output_box.configure(background=&quot;red&quot;)
  </code></pre>
  <p>I have ommited the tkinter part, but the interface looks like this:
  <a href="https://i.stack.imgur.com/PgV8a.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>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.
  <a href="https://i.stack.imgur.com/ALpym.png" rel="nofollow noreferrer">enter image description here</a>
  <a href="https://i.stack.imgur.com/Up4HF.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>After reading the subprocess documentation, I have tried to determine the folder where I want the script to execute from with cwd=&quot;C:\iot_flasher&quot; , but this didn't do the trick.</p>
  <p>Really looking forward for some help from someone with experience in python</p>