How to Specify Working Directory for Popen

How can I specify working directory for popen

subprocess.Popen takes a cwd argument to set the Current Working Directory; you'll also want to escape your backslashes ('d:\\test\\local'), or use r'd:\test\local' so that the backslashes aren't interpreted as escape sequences by Python. The way you have it written, the \t part will be translated to a tab.

So, your new line should look like:

subprocess.Popen(r'c:\mytool\tool.exe', cwd=r'd:\test\local')

To use your Python script path as cwd, import os and define cwd using this:

os.path.dirname(os.path.realpath(__file__)) 

Subprocess changing directory

What your code tries to do is call a program named cd ... What you want is call a command named cd.

But cd is a shell internal. So you can only call it as

subprocess.call('cd ..', shell=True) # pointless code! See text below.

But it is pointless to do so. As no process can change another process's working directory (again, at least on a UNIX-like OS, but as well on Windows), this call will have the subshell change its dir and exit immediately.

What you want can be achieved with os.chdir() or with the subprocess named parameter cwd which changes the working directory immediately before executing a subprocess.

For example, to execute ls in the root directory, you either can do

wd = os.getcwd()
os.chdir("/")
subprocess.Popen("ls")
os.chdir(wd)

or simply

subprocess.Popen("ls", cwd="/")

Python - Change working directory in subprocess.call syntax

I'm suspecting that if the ./ isn't passed to subprocess, on some systems the command may not be found (security issues, . not in system path)

The other issue could be a wrong directory/command not really in the directory.

Obviously I couldn't test that, but that's a safe way (and with error checking) to execute a command in a directory, so it should fix your issue, or at least pinpoint a path problem:

command_dir = '/path/to/sh_script_dir'
command_file = os.path.join(command_dir,"script_name.sh")
if not os.path.isfile(command_file):
raise Exception("{}: no such file".format(command_file))

subprocess.call([command_file,var1,var2], cwd = command_dir)
  • compose your command file with the full path (stored in a variable)
  • test this full path existence prior to running your command (helps to track down a path error)
  • don't use shlex.split when you can pass the list of arguments instead

python subprocess popen set home directory as cwd

I've simplified your code for clarity.


With Python 3.6 or higher you can do the following:

import subprocess, pathlib
subprocess.Popen(['echo', 'test'], cwd=pathlib.Path.home())

With Python 3.5 you need to wrap Path.home() into str():

import subprocess, pathlib
subprocess.Popen(['echo', 'test'], cwd=str(pathlib.Path.home()))

With any Python version below 3.5 you can use:

import os, subprocess
subprocess.Popen(['echo', 'test'], cwd=os.path.expanduser('~'))

Python Popen adding current directory to arguments?

you're working too hard for this. This Popen syntax:

subprocess.Popen(["vmrun", "start", vm_path])

already quotes arguments against blanks (as opposed to the one where you painstakingly compose your own command line as a single string, I don't recommend that)

Since you also add quotes, it passes the quotes litterally and it fails. Just do:

vm_path = vm_root + "XP VM 1/XP VM 1.vmx"

or maybe better:

vm_path = os.path.join(vm_root,"XP VM 1/XP VM 1.vmx")

and you'll be fine.

Python 3: subprocess, changing directory

I have discovered the answer with some help from various stackoverflow posts on the topic as well as stumbling through possible solutions. It was not easy!

self.relativePath = os.path.dirname(sys.argv[0])

self.relativePath1 = self.relativePath + '\\your_subdirectoryHERE\\'

Be sure to include double slashes, to match os.path.dirname(sys.argv[0])

self.process = subprocess.Popen(self.relativePath1 + 'flare.exe -command', cwd=self.relativePath1)


Related Topics



Leave a reply



Submit