Home >Backend Development >Python Tutorial >How Can I Replace the Deprecated `execfile()` Function in Python 3?
Alternative to execfile in Python 3
In Python 3, the convenient execfile() function has been deprecated. This has raised concerns among developers who relied on its ability to execute external scripts.
Question:
It seems that Python 3 has eliminated all the easy options for swiftly loading a script, such as execfile(). Am I overlooking a simple solution?
Answer:
Fortunately, there is an alternative method:
To replace:
execfile("./filename")
With:
exec(open("./filename").read())
Further Information:
Refer to the following resources for additional details:
The above is the detailed content of How Can I Replace the Deprecated `execfile()` Function in Python 3?. For more information, please follow other related articles on the PHP Chinese website!