Categories
Python Answers

How to use execfile in Python 3?

Spread the love

Sometimes, we want to use execfile in Python 3.

In this article, we’ll look at how to use execfile in Python 3.

How to use execfile in Python 3?

To use execfile in Python 3, we call exec.

For instance, we write

exec(open("./filename").read())

to open the ./filename file with open and read it with read.

And then we run the opened file with exec.

Conclusion

To use execfile in Python 3, we call exec.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *