Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run program in Python shell

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?

like image 536
daniel__ Avatar asked Sep 14 '11 18:09

daniel__


People also ask

Can I use Python in shell script?

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.

How do I run a .py file directly?

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.


1 Answers

Use execfile for Python 2:

>>> execfile('C:\\test.py') 

Use exec for Python 3

>>> exec(open("C:\\test.py").read()) 
like image 110
phihag Avatar answered Sep 20 '22 21:09

phihag