Files and terminal
- Lesson 1
- 25 min
- Module 1: Computer and environment basics
Lesson 1 of 18 · Python for Practical AI EngineeringFiles and terminal
1. Computer and environment basics
2. Values and control flow
3. Functions and program structure
4. Files, data and external services
5. Debugging and reliability
6. From notebook to maintainable project
What you will be able to do
Make the project folder for this course in the terminal, and know where you are at each step. You run every Python program from the terminal, and the program looks for its files from the folder you are in.
Before you startWhat to know and to have ready
What you need to know
- Nothing. This is the first lesson of the course. If you did Terminal bridge in the Git course, the first part is a review. The new parts are arguments and exit codes.
What you need on your computer
- A computer with macOS, Windows 10 or 11, or Linux. You open its terminal in Get a terminal.
- Nothing to install. In this lesson, you make the project folder
~/projects/ticket-cleanerand itsdatafolder, which every later lesson uses.
Folders and paths
Section titled “Folders and paths”Your files are in folders, and folders can contain other folders. This lesson makes the folder for the course project, ticket-cleaner. At the end of the lesson, it looks like this:
amira ← your home folder└── projects └── ticket-cleaner └── dataA path is the address of a file or folder. It names each folder on the way to the item. There are two kinds of path:
- An absolute path starts at the top of the disk. It points to the same place from anywhere. For example:
/Users/amira/projects/ticket-cleaner. - A relative path starts from the folder you are in now. If you are in
projects, the relative pathticket-cleaner/datapoints to thedatafolder.
Two short names help with relative paths. .. (two dots) means “the folder above this one”. . (one dot) means “this folder”. Your home folder has the short name ~ (a tilde), so ~/projects means “the projects folder in my home folder”.
The home folder has a different path on each system:
| System | Home folder | Path separator |
|---|---|---|
| macOS | /Users/amira |
/ |
| Linux | /home/amira |
/ |
| Windows | C:\Users\amira |
\ (PowerShell also accepts /) |
You see your own user name instead of amira.
Get a terminal
Section titled “Get a terminal”A terminal is a window where you type commands instead of clicking. You type a command, press Enter, and the computer shows the result as text.
- Press Cmd+Space to open Spotlight.
- Type
Terminal, then press Enter.
- Open the Start menu.
- On Windows 11, type
Terminal. On Windows 10, typePowerShell. - Press Enter.
Both open PowerShell, the Windows terminal that this course uses. The prompt starts with PS, for example PS C:\Users\amira>.
- Press Ctrl+Alt+T.
- If nothing opens, open your applications menu and search for
Terminal.
The terminal shows a prompt: a short line of text that ends in $, % or >. The prompt means that the terminal is ready for a command. In this course, you type the text after the prompt. You do not type the prompt.
The terminal is always in one folder. This folder is the working directory. A command acts on the working directory, unless you give it a different path. A new terminal usually starts in your home folder.
Commands and arguments
Section titled “Commands and arguments”A command is a word, often followed by arguments. An argument tells the command what to act on. A space separates the command from each argument.
cd projects/ticket-cleaner| Part | Value | Meaning |
|---|---|---|
| Command | cd |
Change the working directory. |
| Argument | projects/ticket-cleaner |
The folder to change to. Here, a relative path. |
Some arguments start with - or --. These are options: they change how the command works. You use one in the next lesson: python3 --version asks Python for its version number instead of running a program.
This lesson uses four commands. They work in all three terminals:
| Command | What it does |
|---|---|
pwd |
Shows the working directory (“print working directory”). |
ls |
Lists the files and folders in the working directory. |
mkdir name |
Makes a new folder called name. |
cd path |
Changes the working directory to path. |
Worked example: make the project folder
Section titled “Worked example: make the project folder”Amira makes the project folder. First, she checks where the terminal starts:
pwd/Users/amiraPath----C:\Users\amira/home/amiraThe terminal is in her home folder. She makes the two folders, one in the other, and moves into the second one:
mkdir projectscd projectsmkdir ticket-cleanercd ticket-cleanerpwd/Users/amira/projects/ticket-cleanerEach cd moved one level down, so the path got longer by one folder each time. Now she goes one level up with .., and back down again with a relative path:
cd ..pwdcd ticket-cleaner/Users/amira/projectsExit codes: did the command work?
Section titled “Exit codes: did the command work?”When a command ends, it gives the terminal a number called the exit code. An exit code of 0 means success. Any other number means that the command failed.
You usually see an error message when a command fails, so why does the number matter? Because programs read it, not people. For example, an automatic test system runs your tests and stops the release when the exit code is not 0. Your Python programs also give an exit code, and in Module 6 you choose it yourself.
Amira asks ls to list a folder that does not exist, and then shows the exit code of that command:
ls missingecho $?ls: missing: No such file or directory1ls missing$?ls : Cannot find path 'C:\Users\amira\projects\ticket-cleaner\missing' because it does not exist.(more lines about where the error occurred)FalsePowerShell shows True or False for its own commands. For a program such as Python, $LASTEXITCODE shows the number. You use it in the next lesson.
ls missingecho $?ls: cannot access 'missing': No such file or directory2$? holds the exit code of the last command only. If you run a different command first, $? shows the exit code of that command instead.
Guided practice: make your project folder
Section titled “Guided practice: make your project folder”Make the folders that you use in the rest of this course.
-
Open a new terminal.
-
Show the working directory:
Terminal window pwdThe output is your home folder.
-
Make a
projectsfolder. If you have aprojectsfolder already, skip this step.Terminal window mkdir projects -
Make the project folder, and move into it with one relative path:
Terminal window mkdir projects/ticket-cleanercd projects/ticket-cleaner -
Make a folder for data files:
Terminal window mkdir data -
List the contents of the project folder:
Terminal window lsThe output shows
data. In PowerShell, the output is a table with aNamecolumn. -
Show the exit code of
ls. On macOS and Linux, runecho $?. In PowerShell, run$?.The output is
0orTrue, becauselsworked.
Your turn: move with paths
Section titled “Your turn: move with paths”Do these three tasks. After each one, run pwd to check your result.
- Move into the
datafolder with one relative path. The path ends with/ticket-cleaner/data(or\ticket-cleaner\dataon Windows). - Go back up to
ticket-cleanerwith one command. - Open a new terminal. Move to
ticket-cleanerwith one command that works from any folder.
Show a hint for task 3
A path that starts with ~ starts at your home folder, so it works from any working directory: cd ~/projects/ticket-cleaner. The full absolute path also works, for example cd /Users/amira/projects/ticket-cleaner.
Common mistake
No such file or directory
Symptom: you type cd ticket-cleaner, and the terminal shows an error like one of these:
cd: no such file or directory: ticket-cleanerbash: cd: ticket-cleaner: No such file or directorycd : Cannot find path 'C:\Users\amira\ticket-cleaner' because it does not exist.Cause: a relative path starts from the working directory. Here, the working directory is the home folder, and ticket-cleaner is not in the home folder. It is in projects. The same error occurs if the name is not exactly the same: on macOS and Linux, Ticket-cleaner and ticket-cleaner are different folders.
Fix:
- Run
pwdto see where you are. - Run
lsto see what is here. - Use a relative path from this folder (
cd projects/ticket-cleaner), or a path from your home folder (cd ~/projects/ticket-cleaner).
You see this error again in the next lesson, when Python cannot find a script. The cause and the fix are the same.
Check your understanding
The working directory is ~/projects/ticket-cleaner/data. You run cd .. and then cd .. again. What does pwd show? Predict first, then check it in your terminal.
Show the answer
Your home folder followed by /projects, for example /Users/amira/projects. Each cd .. moves up one level: first to ticket-cleaner, then to projects.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
You run ls notes.txt in a folder that has no file called notes.txt. Then you run echo $? two times. What does each echo show, and why?
Show the answer
The first echo $? shows a number that is not 0, for example 1 or 2, because ls failed. The second shows 0: $? now holds the exit code of the first echo, which worked. Always read the exit code directly after the command that you want to check.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Practical check: open a new terminal and move to your project folder. Prove that you are in the correct folder and that it contains a data folder.
Show the answer
Run cd ~/projects/ticket-cleaner, then pwd and ls. The pwd output ends with projects/ticket-cleaner (with \ on Windows), and the ls output shows data. If you get No such file or directory, use the steps in the common mistake above.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- The terminal is always in one folder, the working directory.
pwdshows it. - An absolute path works from anywhere. A relative path starts from the working directory.
..is the folder above, and~is your home folder. - A command can take arguments, separated by spaces. Options start with
-or--. - Every command ends with an exit code.
0means success. On macOS and Linux,echo $?shows it. In PowerShell,$?showsTrueorFalse.
Next, you install Python and run your first script from the ticket-cleaner folder.