Skip to content

Files and terminal

  • Lesson 1
  • 25 min
  • Module 1: Computer and environment basics
Lesson 1 of 18 · Python for Practical AI EngineeringFiles and terminal

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 readyAbout 25 minutes

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-cleaner and its data folder, which every later lesson uses.

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:

Folder tree
amira ← your home folder
└── projects
└── ticket-cleaner
└── data

A 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 path ticket-cleaner/data points to the data folder.

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.

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.

  1. Press Cmd+Space to open Spotlight.
  2. Type Terminal, then press Enter.

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.

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.

Terminal window
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.

Amira makes the project folder. First, she checks where the terminal starts:

Terminal
pwd
Output
/Users/amira

The terminal is in her home folder. She makes the two folders, one in the other, and moves into the second one:

Terminal
mkdir projects
cd projects
mkdir ticket-cleaner
cd ticket-cleaner
pwd
Output (macOS)
/Users/amira/projects/ticket-cleaner

Each 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:

Terminal
cd ..
pwd
cd ticket-cleaner
Output (macOS)
/Users/amira/projects

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:

Terminal
ls missing
echo $?
Output
ls: missing: No such file or directory
1

$? holds the exit code of the last command only. If you run a different command first, $? shows the exit code of that command instead.

Make the folders that you use in the rest of this course.

  1. Open a new terminal.

  2. Show the working directory:

    Terminal window
    pwd

    The output is your home folder.

  3. Make a projects folder. If you have a projects folder already, skip this step.

    Terminal window
    mkdir projects
  4. Make the project folder, and move into it with one relative path:

    Terminal window
    mkdir projects/ticket-cleaner
    cd projects/ticket-cleaner
  5. Make a folder for data files:

    Terminal window
    mkdir data
  6. List the contents of the project folder:

    Terminal window
    ls

    The output shows data. In PowerShell, the output is a table with a Name column.

  7. Show the exit code of ls. On macOS and Linux, run echo $?. In PowerShell, run $?.

    The output is 0 or True, because ls worked.

Do these three tasks. After each one, run pwd to check your result.

  1. Move into the data folder with one relative path. The path ends with /ticket-cleaner/data (or \ticket-cleaner\data on Windows).
  2. Go back up to ticket-cleaner with one command.
  3. Open a new terminal. Move to ticket-cleaner with 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-cleaner
bash: cd: ticket-cleaner: No such file or directory
cd : 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:

  1. Run pwd to see where you are.
  2. Run ls to see what is here.
  3. 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.

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.

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.

Recap

  • The terminal is always in one folder, the working directory. pwd shows 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. 0 means success. On macOS and Linux, echo $? shows it. In PowerShell, $? shows True or False.

Next, you install Python and run your first script from the ticket-cleaner folder.

Next: Install and verify Python