run program in Python shell


I have a demo file: In the Windows Console I can run the file with: C:\>

How can I execute the file in the Python Shell instead?

5/27/2018 5:09:45 PM

Accepted Answer

Use execfile for Python 2:

>>> execfile('C:\\')

Use exec for Python 3

>>> exec(open("C:\\").read())
9/28/2017 8:15:23 PM

If you're wanting to run the script and end at a prompt (so you can inspect variables, etc), then use:

python -i

That will run the script and then drop you into a Python interpreter.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow