The subprocess module is a very good solution.

import subprocess
p = subprocess.Popen([command, argument1,...], cwd=working_directory)
p.wait()

It has also arguments for modifying environment variables, redirecting input/output to the calling program, etc.

Answer from hynekcer on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › running a shell command from python to execute a file in different directory
r/learnpython on Reddit: running a shell command from python to execute a file in different directory
November 22, 2021 -

Can't tell where I messed this up... I am using pycharm and the folder with all of the files on it is on my desktop....

os.system("cd C:\\softwares\\someDirectory\\subdirectory\\bin execute.sql")

Am I doing this correctly? If I open up the command prompt and just cd to the directory and run the command it runs... but when I try to CD and run it from my python script it spits out the following error

The system cannot find the path specified.

thoughts?

Discussions

python - How to change the working directory for a shell script - Unix & Linux Stack Exchange
I have a Python script that looks files up in a relative directory. For example: the Python script is in /home/username/projectname/. I have a file that is being called within the Python script t... More on unix.stackexchange.com
🌐 unix.stackexchange.com
July 8, 2012
subprocess - Python run shell command over specific folder - Stack Overflow
And I want to run a shell command on a specific folder. ... And I also get the result strings. I've tried like below. result = subprocess.Popen("cd path/to/folder & command") But there is an error. File "C:\Python27_x64\lib\subprocess.py", line 711, in __init__ errread, errwrite) File ... More on stackoverflow.com
🌐 stackoverflow.com
January 14, 2020
How to run shell commands in different directory using python subprocess command? - Stack Overflow
I am having issues changing the ... from the python code from subprocess. ... The subprocess methods all accept a cwd keyword argument. import subprocess d = subprocess.check_output( ['ls'], cwd='/home/you/Desktop') Obviously, replace /home/you/Desktop with the actual directory you want. Most well-written shell commands will not require you to run them in any ... More on stackoverflow.com
🌐 stackoverflow.com
Calling a System/Shell command with Python - Using the Subprocess module
Hey Everyone 🍻, Pretty simple tutorial here on how to send commands to the shell using the subprocess module. I made this tutorial because when I started learning Python, this module was a big help in creating scripts that helped me automate tasks. Unfortunately, before I found out about the subprocess module I was just using os.system and ran into a lot of problems. Looking forward to everyone's feedback! More on reddit.com
🌐 r/Python
6
13
February 23, 2021
🌐
Stack Abuse
stackabuse.com › executing-shell-commands-with-python
Executing Shell Commands with Python
January 6, 2023 - In this script, we create two variables that store the result of executing commands that change the directory to the home folder, and to a folder that does not exist. Running this file, we will see: $ python3 cd_return_codes.py `cd ~` ran with exit code 0 sh: line 0: cd: doesnotexist: No such file or directory `cd doesnotexist` ran with exit code 256
🌐
Python
docs.python.org › 3 › using › cmdline.html
1. Command line and environment — Python 3.14.3 documentation
Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code. If this option is given, the first element of sys.argv will be "-c" and the current directory will be added to the start of sys.path (allowing modules in that directory to be imported as top level modules). Raises an auditing event cpython.run_command with argument command.
Find elsewhere
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
See run() for details. Added in version 3.7: text was added as a more readable alias for universal_newlines. Changed in version 3.12: Changed Windows shell search order for shell=True. The current directory and %PATH% are replaced with %COMSPEC% and %SystemRoot%\System32\cmd.exe.
Top answer
1 of 4
3

First, you should pass a list to Popen, not a string. Each element of the list is one fragment of the commands you want to run. In your case it should be:

proc = subprocess.Popen(['cd', 'path/to/folder', '&', 'command'])

Second, if your command is a system command like cmd. You need to tell Python to use the system shell.

proc = subprocess.Popen(['cd', 'path/to/folder', '&', 'command'], shell=True)

Third, it looks like you want to capture the output from the command. Right now anything that the command does will be written to screen as if you had run the command from the shell. To get Python to capture the outputs, we need to redirect them from writing to the screen back to Python. We do this using subprocess.PIPE for the stdout and stderr streams.

proc = subprocess.Popen(['cd', 'path/to/folder', '&', 'command'], 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

Lastly, using Popen returns a Popen object. You don't get the results right away, because the system might be churning away behind the scenes. Using Popen allows your code to keep running (Popen is non-blocking). To get the output, you need to run the communicate method. This returns the output from the output and error streams.

out, err = proc.communicate()

Example:

Change directory and list contents.

proc = subprocess.Popen(['cd', 'Documents/Projects', '&', 'dir'], 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()
print(out.decode('latin-1'))
# prints
 Volume in drive C is OS
 Volume Serial Number is DE6D-0D88

 Directory of C:\Users\james\Documents\Projects

01/04/2020  01:50 PM    <DIR>          .
01/04/2020  01:50 PM    <DIR>          ..
11/15/2019  09:05 PM    <DIR>          .vscode
01/09/2020  11:15 PM    <DIR>          CondaDeps
01/03/2020  09:22 PM    <DIR>          Django
12/21/2019  10:52 PM    <DIR>          FontCluster
12/20/2019  03:56 PM                70 fontcluster.code-workwspace.code-workspace
11/08/2019  03:01 PM    <DIR>          ScholarCrawler
12/30/2019  10:48 AM                56 scholarcrawler.code-workspace
07/24/2019  09:56 PM    <DIR>          ThinkStats2
               2 File(s)            126 bytes
               8 Dir(s)  415,783,694,336 bytes free
2 of 4
3

The Popen has a cwd keyword argument .

result = subprocess.Popen("command", cwd="path/to/folder")

And the error you are getting is because the Popen's shell keyword argument is set to false by default, so it doesn't know the cd command.

🌐
Linux Handbook
linuxhandbook.com › execute-shell-command-python
How to Execute Bash Shell Commands with Python
June 28, 2022 - A slightly better way of running shell commands in Python is using the subprocess module. If you want to run a shell command without any options and arguments, you can call subprocess like this: ... The call method will execute the shell command. You’ll see the content of the current working directory ...
🌐
Reddit
reddit.com › r/python › calling a system/shell command with python - using the subprocess module
r/Python on Reddit: Calling a System/Shell command with Python - Using the Subprocess module
February 23, 2021 - Pretty simple tutorial here on how to send commands to the shell using the subprocess module. I made this tutorial because when I started learning Python, this module was a big help in creating scripts that helped me automate tasks.
Top answer
1 of 16
5970

Use subprocess.run:

import subprocess

subprocess.run(["ls", "-l"]) 

Another common way is os.system but you shouldn't use it because it is unsafe if any parts of the command come from outside your program or can contain spaces or other special characters, also subprocess.run is generally more flexible (you can get the stdout, stderr, the "real" status code, better error handling, etc.). Even the documentation for os.system recommends using subprocess instead.

On Python 3.4 and earlier, use subprocess.call instead of .run:

subprocess.call(["ls", "-l"])
2 of 16
3674

Here is a summary of ways to call external programs, including their advantages and disadvantages:

  1. os.system passes the command and arguments to your system's shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example:

    os.system("some_command < input_file | another_command > output_file")
    

    However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, et cetera. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs.

  2. os.popen will do the same thing as os.system except that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don't need to worry about escaping anything. Example:

    print(os.popen("ls -l").read())
    
  3. subprocess.Popen. This is intended as a replacement for os.popen, but has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you'd say:

    print subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read()
    

    instead of

    print os.popen("echo Hello World").read()
    

    but it is nice to have all of the options there in one unified class instead of 4 different popen functions. See the documentation.

  4. subprocess.call. This is basically just like the Popen class and takes all of the same arguments, but it simply waits until the command completes and gives you the return code. For example:

    return_code = subprocess.call("echo Hello World", shell=True)
    
  5. subprocess.run. Python 3.5+ only. Similar to the above but even more flexible and returns a CompletedProcess object when the command finishes executing.

  6. os.fork, os.exec, os.spawn are similar to their C language counterparts, but I don't recommend using them directly.

The subprocess module should probably be what you use.

Finally, please be aware that for all methods where you pass the final command to be executed by the shell as a string and you are responsible for escaping it. There are serious security implications if any part of the string that you pass can not be fully trusted. For example, if a user is entering some/any part of the string. If you are unsure, only use these methods with constants. To give you a hint of the implications consider this code:

print subprocess.Popen("echo %s " % user_input, stdout=PIPE).stdout.read()

and imagine that the user enters something "my mama didnt love me && rm -rf /" which could erase the whole filesystem.

🌐
CodeFatherTech
codefather.tech › home › blog › how to execute a shell command in python [step-by-step]
How to Execute a Shell Command in Python [Step-by-Step]
December 8, 2024 - We have seen lots of different ways to execute a shell command in Python. Well done for completing this tutorial! The recommended way to invoke shell commands is definitely subprocess.run unless you are not using Python 3.5+. In that case you can use subprocess.Popen.
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 01.05-ipython-and-shell-commands.html
IPython and Shell Commands | Python Data Science Handbook
As an example, here is a sample of a Linux/OSX shell session where a user explores, creates, and modifies directories and files on their system (osx:~ $ is the prompt, and everything after the $ sign is the typed command; text that is preceded by a # is meant just as description, rather than something you would actually type in): osx:~ $ echo "hello world" # echo is like Python's print function hello world osx:~ $ pwd # pwd = print working directory /home/jake # this is the "path" that we're sitting in osx:~ $ ls # ls = list working directory contents notebooks projects osx:~ $ cd projects/ # cd = change directory osx:projects $ pwd /home/jake/projects osx:projects $ ls datasci_book mpld3 myproject.txt osx:projects $ mkdir myproject # mkdir = make new directory osx:projects $ cd myproject/ osx:myproject $ mv ../myproject.txt ./ # mv = move file.
🌐
Python Engineer
python-engineer.com › posts › python-execute-system-command
How to execute a Program or System Command from Python - Python Engineer
To execute a child program in a new process, use subprocess.Popen(): import subprocess subprocess.Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."]) Similar to .run()it can take a lot of optional arguments which can be found here. Both runand Popen can take the keyword argument shell. If shell is True, the specified command will be executed through the shell.