I have a demo file: test.py
. In the Windows Console I can run the file with: C:\>test.py
How can I execute the file in the Python Shell instead?
Python allows you to execute shell commands, which you can use to start other programs or better manage shell scripts that you use for automation. Depending on our use case, we can use os. system() , subprocess. run() or subprocess.
Type cd PythonPrograms and hit Enter. It should take you to the PythonPrograms folder. Type dir and you should see the file Hello.py. To run the program, type python Hello.py and hit Enter.
Use execfile for Python 2:
>>> execfile('C:\\test.py')
Use exec for Python 3
>>> exec(open("C:\\test.py").read())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With