Post

PythonPython

v
victoria mamidi

Posted on 27th February 2024|36 views

0
votes

Python Run Another Python File

Is it possible to make one python file to run another?

 

Answers
P
Surya kumar

Posted on 27th February 2024

There are various methods to make one python file to run another they are:

Method 1:

Utilize it as a module. import the file you need to run also run its functions. For instance, say you require to import fileY.py inside fileX.py, allowing the files are within the same directory, within fileX you would write like this:

import fileY

Now within fileX, you can call any function you want within fileY like:

fileY.some_func()

Method 2: 

Use exec command:

execfile('file.py')

Method 3:

We can spawn any new process by utilizing the os.system command

os.system('python any_file.py')

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos