Creating and Saving Python Files
Running a Python script means telling the Python interpreter to read and execute code from a file, rather than typing code interactively line by line.
From Saved File to Running Program
A Python script is a file containing Python code. Running the script means telling the Python interpreter to read and execute the code from that file instead of entering the code interactively, one line at a time. The key instruction is given at the operating system command prompt: python <filename>.
The Execution Trace
When you run a script, the interpreter follows a straightforward sequence. It opens the file, reads all of its lines, executes the lines in order from top to bottom, produces any output requested by the code, and then exits. The filename in the command tells the interpreter which saved file should supply the instructions.
Tracing a File-Based Run
Suppose a saved file is named lesson_file and the command prompt receives the command python lesson_file. What sequence does the interpreter follow?
1. Receive the filename: The operating system command prompt passes lesson_file to the Python interpreter as the file to run.
2. Open and read: The interpreter opens the named file and reads its lines.
3. Execute in order: The interpreter executes the file's lines from top to bottom.
4. Produce output: Any output produced by the executing code appears as the script runs.
5. Reach the end: After the final line has been executed, the interpreter automatically exits.
The command starts one complete top-to-bottom file execution. No quit() command is needed at the end.
File Execution and Interactive Mode
Interactive mode and file-based execution differ in where the interpreter gets its instructions. In interactive mode, you type Python code line by line. In file-based execution, the interpreter reads the code from a saved file after you provide the filename at the command prompt.
| Interactive mode | File-based execution |
|---|---|
| Python code is typed line by line. | Python code is read from a saved file. |
| The interpreter receives code interactively. | The command prompt supplies a filename to the interpreter. |
| The learner provides the next line during the interaction. | The interpreter reads and executes the file's lines in order. |
Why quit() Is Unnecessary
A script does not need a quit() command to finish. The Python interpreter automatically exits when it reaches the end of the file. In other words, reaching the final line completes the file-based execution process.
Mistakes Beginners Make
Entering the filename without telling Python to run it.
The documented command for running a script supplies the filename to the Python interpreter using the form python <filename>.
Fix:
Use the Python command followed by the filename.Expecting file-based execution to wait for each line interactively.
In file-based execution, the interpreter reads the file and executes its lines in order.
Fix:
Think of the saved file as the interpreter's input for the complete top-to-bottom run.Adding quit() because the script must explicitly terminate.
The interpreter automatically exits when it reaches the end of the file.
Fix:
Allow the script to finish at its final line; quit() is not needed in the script file.
Check Your Understanding
A file named practice_file has been saved. Describe what happens after you enter python practice_file at the operating system command prompt. Include the interpreter's input source, the execution order, and what happens after the final line.
Hints
- Start with the filename supplied to the Python interpreter.
- Recall the order: open the file, read its lines, execute them from top to bottom, and produce output.
- End by explaining why quit() is unnecessary.
What do you think happens?
After the interpreter executes the final line of a saved Python script, what happens?
Reveal answer
Answer: It automatically exits.
The Python interpreter automatically exits when it reaches the end of the file, so a quit() command is not needed in a script.
Key Takeaways
- Run a saved Python script by entering python <filename> at the operating system command prompt.
- File-based execution reads code from a file, while interactive mode receives Python code line by line.
- During a script run, the interpreter opens the file, reads its lines, executes them from top to bottom, and produces output.
- The interpreter automatically exits when it reaches the end of the file.
- A script does not need a quit() command to terminate.
Key Takeaways
- The command python <filename> tells the Python interpreter which saved file to execute.
- A script run is file-based: Python reads the file and executes its lines from top to bottom.
- Interactive mode differs because code is entered line by line instead of being read from a saved file.
- When the interpreter reaches the end of a script file, it exits automatically, so quit() is unnecessary.