How to Run and Control External Programs in Python on Windows
This guide explains four Python techniques for executing and managing external Windows programs—including os.system, win32api ShellExecute, win32process CreateProcess, and ctypes calls to kernel32.dll—providing code signatures, parameter details, and practical examples.
Method 1: Using os.system()
The os.system(command) function runs an external program or script; command is a string containing the executable and any arguments separated by spaces.
command – the command line to execute, with parameters separated by spaces.
Method 2: Using ShellExecute via win32api
Another way is to call ShellExecute(hwnd, op, file, args, dir, show) from the win32api module. Parameters:
hwnd – handle of the parent window (0 if none)
op – operation to perform, e.g., "open", "print", or empty
file – the program or script to run
args – arguments passed to the program (empty for files)
dir – initial working directory of the program
show – whether to display the window
Method 3: Creating a Process with win32process
For finer control you can create a process using
CreateProcess(appName, cmdLine, procAttr, threadAttr, inheritHandle, creationFlags, newEnv, currentDir, attr). Parameters:
appName – executable file name
cmdLine – command‑line arguments
procAttr – process security attributes
threadAttr – thread security attributes
inheritHandle – inheritance flag
creationFlags – creation flags
newEnv – environment block (or None)
currentDir – current directory for the process
attr – additional attributes
To terminate a process you can call TerminateProcess(handle, exitCode), where handle is the process handle and exitCode is the exit code.
You may also wait for a thread to finish with WaitForSingleObject(handle, milliseconds); milliseconds is the timeout (‑1 means infinite).
Method 4: Calling kernel32.dll Functions with ctypes
The ctypes module lets Python call functions in dynamic libraries such as kernel32.dll. It provides C‑compatible data types and can invoke functions like MessageBoxA from user32.dll. The module works on Windows, Linux, and macOS.
Example: calling MessageBoxA from user32.dll to display a message box.
Note: Install the pywin32 package (e.g., pywin32‑216.win‑amd64‑py2.7) to use the win32 APIs on a 64‑bit Windows system.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
