Scripts and the Terminal
Create a Python script as a plain-text file, find it from the terminal, and run it with a verified interpreter command. Learn how the current working directory, filename, and command determine which source code Python executes.
A Python script is a plain-text file that contains Python code. Saving code in a file lets you inspect it, run it again, and keep it after the terminal closes.
In this lesson, you will create hello.py, place the terminal in the same
folder, and ask the Python interpreter to run the file. The complete workflow
is:
- create a folder and a plain-text file;
- write Python code in the file;
- confirm that the terminal is looking at the correct folder;
- run the file with the Python command that worked in the previous lesson;
- compare the output with the code.
Create a Folder and Script
Use your file manager to create a folder named python-practice. A
directory is another name for a folder.
Inside that folder, create a plain-text file named hello.py. The .py
extension tells people and software that the file contains Python source code.
Enter these two lines:
Save the file. Check that its complete name is hello.py, not hello.py.txt.
Some operating systems hide filename extensions, so display the file's details
if the complete name is not visible.
You can use any editor that saves plain text. If you use TextEdit on macOS,
choose Format → Make Plain Text before saving. If you use Notepad on
Windows and it tries to add .txt, choose All files as the file type when
saving. A code editor can also save the file, but you do not need to install
one for this lesson.
The Current Working Directory
The terminal always has a current working directory: the folder in which it
looks for files unless you provide another path. We will place the terminal in
python-practice before running hello.py.
Many file managers have an Open in Terminal action for a folder. If yours
does, use it on python-practice.
Otherwise, open a terminal and type cd followed by a space. cd means
change directory. Drag the python-practice folder into the terminal if
your system supports this, then press Enter. Dragging the folder inserts its
path, including any characters needed for spaces in folder names.
You can now ask the terminal to print its current location. pwd means
print working directory:
pwd
PowerShell also accepts pwd. The printed path should end with
python-practice.
Next, list the files in that folder.
On macOS or Linux:
ls
On Windows PowerShell:
dir
The list should contain hello.py. If it does not, either the terminal is
looking at a different folder or the file was saved somewhere else. Resolve
that difference before running Python.
Run the Script
Use the Python command that printed a suitable version in the previous lesson.
If your command is python3, run:
python3 hello.py
If your command is python, run:
python hello.py
If your command is py, run:
py hello.py
Each command has two parts separated by a space:
python3,python, orpystarts the Python interpreter;hello.pyis an argument that tells the interpreter which script to run.
Python reads the file from top to bottom. The terminal should show:
Hello from a file
2 + 3 = 5
The source file contains print, quotation marks, parentheses, and the
expression 2 + 3. The output contains only the text and values that the two
calls to print() display.
When Python Cannot Find the File
Suppose the file is named hello.py, but the command says:
python3 helo.py
The filename is missing an l. Python may report that it cannot open the file.
This is a filename or path problem, not a problem with the Python instructions
inside the file.
If Python cannot find a script:
- use
pwdto inspect the current working directory; - use
lsordirto inspect the filenames there; - compare the listed filename with the filename in the run command;
- correct the folder or spelling, then run the command again.
Read the evidence before reinstalling Python or changing the script's contents. A working interpreter can still receive the wrong file path.
Why Saved Scripts Are Useful
A script keeps the source code between runs. You can close the terminal, return later, and run the same file again. You can also inspect exactly what changed between two runs.
This makes scripts useful for learning, debugging, and repeatable experiments. They are not the only way to work with Python, the next lesson introduces an interactive session for quick calculations but they provide a durable record of a program.
Exercise: Prepare a small script
Change the value so the output contains score: 8. The same source code could
then be saved in a .py file and run from a terminal.
Prepare a small script
Ready to run.
HintChange the stored value
The print line already supplies the text score:. Change the number assigned
to score.
SolutionAssign eight
Use score = 8 and leave the print line unchanged:
Exercise: Find a missing script
python3 --version works. Running python3 hello.py reports that hello.py
cannot be opened, and ls does not show the file. What should you inspect
first?
Select one choice, then check.
HintUse the evidence already available
The version command proves that the interpreter starts. The file list does
not contain hello.py.
SolutionCompare the two folders
Use pwd to identify the terminal's current working directory. Then find
where hello.py was saved. Move to that folder with cd, or save the file in
the folder already shown by pwd.
What You Can Now Do
You can create a Python source file, place the terminal in its folder, and run the file with a verified interpreter command. The next lesson starts Python without a script so that you can evaluate small expressions interactively.