Running a external program using Python

Refresh

April 2019

Views

28 time

1

I want to launch a external program like notepad.exe using python. I want to have a script that just runs Notepad.exe.

3 answers

0

Would recommend the subprocess module. Just build up a list of arguments like you would run in the terminal or command line if you are on windows then run it.

import subprocess

args = ['path\to\program\here\program.exe']
subprocess.call(args)

Check out the docs here for all of the other process management functionality.

1

It's really simple with Python's builtin os module.
This will start Microsoft Notepad:

import os

# can be called without the filepath, because notepad is added to your PATH
os.system('notepad.exe')

Or if you want to launch any other program just use:

import os

# r for raw-string, so don't have to escape backslashes
os.system(r'path\to\program\here\program.exe')
-1

this can be done using Python OS. Please see the code below for an example.

import os
os.startfile('test.txt')

Startfile will execute the program associated with the file extension.