How to Run External Programs in Python on Windows Using os, win32api, and ctypes

This guide explains multiple methods for executing external programs and scripts from Python on Windows, covering the simple os.system call, the win32api ShellExecute function, creating processes with win32process.CreateProcess, and invoking kernel32.dll functions via ctypes, complete with syntax, parameters, and example code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Run External Programs in Python on Windows Using os, win32api, and ctypes

In Python you can conveniently use the os module to run other scripts or programs, allowing you to reuse functionality without rewriting code. For finer process control you can use the win32process module, and for low‑level control you can call Windows API functions via the ctypes module.

Method 1: Using os.system()

The os.system(command) function runs a command line string. command is the command to execute; arguments can be separated by spaces.

Example:

Method 2: Using ShellExecute from win32api

The ShellExecute(hwnd, op, file, args, dir, show) function launches a program similarly to double‑clicking its icon in Explorer.

hwnd: parent window handle (0 if none)

op: operation such as "open" or "print"

file: program or script to run

args: arguments for the program (empty for files)

dir: working directory

show: whether to display the window

Example:

Method 3: Creating a process with win32process.CreateProcess()

The

CreateProcess(appName, cmdLine, proAttr, threadAttr, InheritHandle, CreationFlags, newEnv, currentDir, Attr)

function creates a new process with detailed control.

appName: executable name

cmdLine: command line arguments

proAttr: process security attributes

threadAttr: thread security attributes

InheritHandle: inheritance flag

CreationFlags: creation flags

newEnv: environment for the new process

currentDir: initial directory

Attr: startup information

Example:

Method 4: Terminating a process

You can end a created process with win32process.TerminateProcess(handle, exitCode) or wait for it to finish using win32event.WaitForSingleObject(handle, milisecond).

handle: process handle

exitCode: exit code to return

milisecond: wait time (-1 for infinite)

Examples:

Method 5: Calling Windows API functions via ctypes

The ctypes module lets Python call functions in DLLs such as kernel32.dll or user32.dll. It provides C‑compatible data types and can be used across Windows, Linux, and macOS.

Example: calling MessageBoxA from user32.dll.

Below is a table of common ctypes data types and their C equivalents.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

subprocessctypeswin32api
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.