Home >Backend Development >Python Tutorial >What's the Python 3 Equivalent of `execfile()`?
Alternative to execfile in Python 3
In previous versions of Python, execfile() provided a convenient way to load and execute external scripts. However, Python 3 discontinued this function, leaving many users wondering for an alternative solution.
Easy Script Loading in Python 3
To overcome the absence of execfile(), Python 3 offers a straightforward alternative: exec(open(file_name).read()). This syntax effectively reads the contents of the external script into a string, then executes it using the exec() function.
For example:
exec(open("./filename").read())
Documentation References
The above is the detailed content of What's the Python 3 Equivalent of `execfile()`?. For more information, please follow other related articles on the PHP Chinese website!