Skip to content
Advertisement

How do I run another file in python?

So I know how to write in a file or read a file but how do I RUN another file?

for example in a file I have this: a = 1 print(a)

How do I run this using another file?

Advertisement

Answer

JavaScript

using subprocess standard lib

JavaScript

or using os standard lib

JavaScript

or extract python code from the file and run it inside your script:

JavaScript

exec is a function that runs python strings exactly how python interpreter runs python files.

IN ADDITION

if you want to see the output of the python file:

JavaScript

EXTRA

for people who are using python2:

JavaScript

exec_file documentation

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement