Python Forum

Full Version: How to make python run other file?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I make python run another file from the local disk?
You could be more specific about file types and task it should to do.
If it's a .py then module or packages is used for that trough import.

subprocess is way for Python to run other files.
So example this will on Windows run ping.exe and we capture output it gives.
import subprocess

out = subprocess.run(['ping', 'google.com'], capture_output=True, encoding='utf-8')
print(out.stdout)
Output:
Pinging google.com [172.217.20.46] with 32 bytes of data: Reply from 172.217.20.46: bytes=32 time=39ms TTL=119 Reply from 172.217.20.46: bytes=32 time=39ms TTL=119 Reply from 172.217.20.46: bytes=32 time=46ms TTL=119 Reply from 172.217.20.46: bytes=32 time=53ms TTL=119 Ping statistics for 172.217.20.46: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 39ms, Maximum = 53ms, Average = 44ms
I am wanting to run another .py
And I use OSX.
If you have code in another Python module or package, then as above, import the functions, classes, etc. and use them.

Also show a code sample if you're still confused.