- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to execute a Python file in Python shell?
To execute a Python file in the python shell, you could use either the execfile method or the exec method.
Example
For example, you want to run a script called my_script.py that only contains the line:
print("Greetings from my_script")
from the python shell, you could simply enter:
>>> execfile('my_script.py') Greetings from my_script
Or you could use the exec method as follows:
>>> exec(open("my_script.py").read()) Greetings from my_script
- Related Articles
- How to clear Python shell?
- How to execute a Javascript using Selenium in Python?
- How do I execute a string containing Python code in Python?
- How to execute a Javascript function in Python with Selenium?
- How to know/change current directory in Python shell?
- Python Interface to Shell Pipelines
- How to read a text file in Python?
- How to edit a file after I shell to a Docker container?
- How can I source a Python file from another Python file?
- How to Count Word Occurrences in a Text File using Shell Script?
- How to open a file to write in Python?
- How to rename a file using Python?
- How to delete a file using Python?
- How to remove a file using Python?
- How to find a file using Python?

Advertisements