Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Files, folders, and the terminal

~30 min

In Chapter 1 you'll save a file called hello.py and type python hello.py. For that to work, Python has to find hello.py, and it looks for it in one particular folder: the one your terminal is standing in. Most of the "it says file not found" trouble beginners hit in their first week comes from that one fact. So before anything else, two skills: how files and folders are named, and how to move around them by typing instead of clicking.

Files, folders, and paths

A file is one saved thing: a song, a photo, a page of writing. Its name usually ends with a dot and a few letters, like .jpg or .py. That ending is called the extension, and it hints at what's inside. Python programs live in files ending in .py. The ending really is part of the name, so notes.py and notes.txt can sit side by side as two separate files.

A folder is a container for files. Folders can hold other folders, like boxes packed inside boxes. Some tools say directory instead of folder; it means the same thing.

A path is a file's address. It lists the folders you'd open, one after another, to reach the file. Sam's Windows laptop keeps a song at C:\Users\Sam\Music\song.mp3. Read it left to right: drive C, then the Users folder, then Sam's folder, then Music, and there's song.mp3. Windows puts a backslash (\) between the names. Macs and Linux use a forward slash (/), so on Sam's Mac the same kind of address reads /Users/sam/Music/song.mp3.

An address that starts from the outermost box (the drive C: on Windows, or the lone / on a Mac or Linux) is called absolute. It works from anywhere, like a full postal address with the country on it. A shorter address like python-course/hello.py is relative. It's like "second door on the left": it only makes sense once you know where you're standing.

The folder you're standing in right now is called the working directory. Two dots, .., mean "the folder one level up from here". A squiggle, ~ (called a tilde), means "my home folder": the folder that belongs to you when you log in. When you later type python hello.py, Python looks for hello.py in the folder you're standing in, and nowhere else.

A file is a named block of data stored on disk: a photo, a document, a Python source file. The part of its name after the last dot, the extension (.py, .txt, .jpg), tells people and programs what kind of content to expect. The extension is part of the name, so notes.py and notes.txt are two different files. A folder (also called a directory) holds files and other folders, and the whole disk forms a tree of folders with one top.

A path is the address of one file or folder: the folders you pass through from a starting point, joined by a separator. Windows uses \ and starts from a drive letter, as in C:\Users\Sam\python-course\hello.py. macOS and Linux use / and start from a single top folder written /, as in /Users/sam/python-course/hello.py on a Mac and /home/sam/python-course/hello.py on Linux. A path that starts at the top of the tree is absolute: it names the same place from anywhere.

A path such as python-course/hello.py has no top of its own. It is relative, read starting from the working directory: the folder a terminal, or a running program, is currently in. Three shorthands appear everywhere: . is the working directory itself, .. is the folder that contains it, and ~ is your home folder, the personal folder your account starts in. So the same relative path can name different files depending on the working directory, while an absolute path cannot. When you type python hello.py, hello.py is a relative path, which is why the working directory decides whether Python finds it.

Works from anywhere, or depends where you are?

Sort each path: does it name the same place from any folder, or does it depend on the working directory?

Loading interactive…

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Correct answer

Also accepted:

Why

Why

A path that begins with a drive letter like C: or with a leading / starts from the top of the tree, so it works from anywhere. python-course/hello.py, hello.py, .., and ../ch01 are all read starting from whatever folder you're in.

Worked example

Following a relative path

  1. 1

    Sam's working directory is /Users/sam. He asks for the file python-course/hello.py.

  2. 2

    A relative path starts from the working directory, so join the two with a /: /Users/sam + / + python-course/hello.py gives /Users/sam/python-course/hello.py. The file is there.

  3. 3

    Now he moves into python-course and asks for python-course/hello.py again. Joined, that's /Users/sam/python-course/python-course/hello.py. There's no python-course folder inside python-course, so the file isn't found.

    same words, new starting point, different answer

  4. 4

    From inside python-course, the right relative path is just hello.py. The absolute path /Users/sam/python-course/hello.py would have worked from both places.

Worked example

Going up with ..

  1. 1

    Sam is in /Users/sam/python-course/ch01 and wants the folder ch00, which sits beside ch01 rather than inside it.

  2. 2

    .. means one folder up. From ch01, that's python-course.

  3. 3

    So ../ch00 means: go up to python-course, then down into ch00. It lands on /Users/sam/python-course/ch00.

  4. 4

    ../.. goes up twice, to /Users/sam.

    On Windows the same trip is written ..\ch00, and PowerShell also accepts ../ch00.

Check your understanding (counts toward your progress)

You're in /Users/sam/python-course/ch01. Where does .. point?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

That's two levels up, ../...

That's ., the working directory itself.

/ is the very top, many levels up.

Why

.. is the folder that contains the working directory, one level up.

.. goes up exactly one folder.

Your working directory is /home/ana. Which file does the relative path python-course/hello.py name?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

That would be reading it from the very top, which a relative path doesn't do.

It starts from /home/ana, not from /home.

The python-course folder in the path can't be skipped.

Why

A relative path is read from the working directory: /home/ana + / + python-course/hello.py.

Join the working directory and the relative path with a /.

Sort each file name by its extension, the part after the last dot. Is it a Python source file?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

The extension is whatever follows the last dot. hello.py.txt ends in .txt, so it's a text file, while notes.v2.py ends in .py. report.py and report.txt are two different files.

Correct answer

Also accepted:

Why

Only the part after the LAST dot counts.

Sam is inside /Users/sam/python-course and asks for python-course/hello.py. It isn't found, though hello.py is right there. Why?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Relative paths work from any folder; they're read from that folder.

Capitals aren't the problem here; the extra folder name is.

Dashes are fine in folder names.

Why

From inside python-course, the extra python-course/ sends the search into a folder that doesn't exist. Just hello.py would work.

Join his working directory with the path he typed.

Practice: Files, folders, and paths (5 problems · doesn’t affect your progress)

What is the working directory?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Python's install folder has nothing to do with it.

A new terminal starts at home, but cd changes it.

That's / or a drive letter, the top of the tree.

Why

The working directory is where you're standing right now; relative paths are read from it.

The folder a terminal or program is currently in

Which separator does a Windows path use between folder names?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

macOS and Linux use /. (PowerShell also accepts it, but Windows paths are written with \.)

A dot starts an extension, as in .py.

A colon follows only the drive letter, as in C:.

Why

Windows writes paths like C:\Users\Ana\python-course, with backslashes.

\ (backslash)

Working directory /home/li. Which file does projects/ch01/hello.py name?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

A relative path doesn't start at the top.

It starts at /home/li, not /home.

Every folder in the path is part of the trip.

Why

Join the working directory and the relative path: /home/li/projects/ch01/hello.py.

/home/li/projects/ch01/hello.py

From /Users/sam/python-course/ch01, where does ../.. point?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

That's one ... There are two.

That would be three levels up.

That's the very top, many levels up.

Why

Each .. goes up one level: first to python-course, then to /Users/sam.

/Users/sam

Challenge: which path finds hello.py correctly both from /home/kai and from /home/kai/python-course?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

That works from /home/kai only; inside python-course it looks for a folder that isn't there.

That works from inside python-course only.

That looks one level up from wherever you are.

Why

Only an absolute path names the same file from every working directory.

/home/kai/python-course/hello.py

Opening a terminal and running a command

A terminal is a window where you give your computer instructions by typing instead of clicking. Each instruction is called a command. You type it, press Enter, and the computer does it. Any reply is printed as text right below. Then a short marker called the prompt appears, and the terminal waits for your next command. The program inside the window that reads what you type is called a shell. Windows uses one called PowerShell, inside an app called Windows Terminal. A Mac has an app simply called Terminal. Linux desktops have one too, usually with "Terminal" in its name. The details differ; the idea is the same everywhere.

A command reads like a short order: a verb first, then the thing it works on. Take mkdir python-course. The verb is mkdir, short for "make directory", which means make a folder. python-course is the name of the new folder.

A terminal is always standing in one folder, its working directory (the folder that short, relative addresses start from). A newly opened terminal stands in your home folder. Four commands do nearly everything you'll need at first:

  • pwd asks "where am I?" It prints the full address of the folder you're in. The letters stand for "print working directory".
  • ls asks "what's in here?" It lists the files and folders.
  • mkdir makes a new folder. You stay where you were.
  • cd means "change directory". It moves you into another folder. cd .. moves you up one level, and cd ~ takes you home.

Two surprises. First, when a command works, it often prints nothing at all, and an empty reply usually means success. Second, a space splits a command into separate words, so cd My Projects looks like two folder names to the shell. Put quotes around a name with a space in it, or better, name your folders without spaces.

A terminal is a window for working with the computer by typing. It runs a shell, a program that reads one line you type (a command), runs it, prints any result, then shows a prompt and waits for the next line. The default shell is PowerShell on Windows (inside the Windows Terminal app), zsh on macOS, and usually bash on Linux. A command starts with its name; the words after it, separated by spaces, say what to act on. In mkdir python-course, mkdir is the command and python-course is the folder it works on.

Four commands cover everything this course needs early on, and they work in all three shells. pwd prints the working directory as an absolute path. ls lists what's in the working directory. mkdir name makes a new folder there, without moving into it. cd path changes the working directory to path, absolute or relative; cd .. moves up one level and cd ~ goes home. A new terminal starts in your home folder. Many commands print nothing when they succeed, so an empty reply is normal. Because a space separates the words of a command, a folder name containing a space must be wrapped in quotes (cd "My Projects"), which is one reason programmers name folders without spaces.

The steps for opening a terminal differ by operating system because each one ships a different terminal app and a different shell, with different prompts and slightly different replies. The four commands themselves are the same everywhere. Open a terminal now and make the folder you'll use in Chapter 1. Pick your operating system; the course remembers your choice.

Press the Windows key, type Terminal, and press Enter. Windows Terminal opens with a PowerShell prompt ending in >. Type these one at a time, pressing Enter after each:

cd ~
mkdir python-course
cd python-course
pwd

~ is your home folder, C:\Users\<your name>. PowerShell's pwd prints a small table with the heading Path, and the path under it should end in \python-course. mkdir also prints a short table about the folder it made. That's PowerShell being chatty, not an error.

Press Cmd+Space, type Terminal, and press Enter. (It also lives in Applications, inside the Utilities folder.) The prompt ends in %. Type these one at a time, pressing Enter after each:

cd ~
mkdir python-course
cd python-course
pwd

~ is your home folder, /Users/<your name>, so pwd prints a path ending in /python-course.

Open your applications menu and search for Terminal. On many desktops, including Ubuntu's, Ctrl+Alt+T opens one directly. The prompt usually ends in $. Type these one at a time, pressing Enter after each:

cd ~
mkdir python-course
cd python-course
pwd

~ is your home folder, /home/<your name>, so pwd prints a path ending in /python-course.

On a Mac, the whole exchange looks like this. The lines starting with $ are what was typed (your prompt will look a little different); the others are the replies.

$ cd ~
$ mkdir python-course
$ cd python-course
$ pwd
/Users/sam/python-course
$ ls
$

ls printed nothing because the new folder is empty. In Chapter 1 you'll save hello.py here, and then ls will list it. Two habits save a lot of typing in every shell. Press Tab partway through a folder name and the shell finishes it for you. Press the up arrow to bring back the command you typed last.

Set up a folder for Chapter 1

Put the commands in order: start from your home folder, make a folder called python-course, go into it, make a folder called ch01 inside it, go into that, and check where you ended up.

Loading interactive…

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Correct answer

Also accepted:

Why

Why

Go home first, then make python-course and step into it, so mkdir ch01 puts the new folder inside python-course. Then enter ch01 and let pwd confirm the path ends in python-course/ch01. cd ch01/python-course has the folders backwards: there is no python-course inside ch01.

Program, data, or command?

Sort each item: is it a Python program (source code), data a program reads, or a command you type into a terminal?

Loading interactive…

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Correct answer

Also accepted:

Why

Why

hello.py and a print line inside a .py file are source code. scores.txt and the photo are data that some program opens. cd, mkdir, and python hello.py are all commands you type at the prompt: python hello.py is the command that starts the interpreter and tells it which program to follow.

Worked example

Where am I, and what's here?

  1. 1

    Open a fresh terminal and type pwd. It prints your home folder, because every new terminal starts there.

  2. 2

    Type cd python-course. Nothing prints. That's success. Type pwd again: the path now ends in python-course.

  3. 3

    Type mkdir ch01, then ls. The list shows ch01. But pwd still ends in python-course, because mkdir made the folder without moving you into it.

    making a folder and entering it are two separate commands

  4. 4

    Type cd ch01 and you're inside. cd .. brings you back up to python-course.

Worked example

When cd can't find the folder

  1. 1

    Priya opens a new terminal and types cd ch01 straight away. Instead of silence, she gets an error saying there's no such folder. The exact wording differs between PowerShell, zsh, and bash.

  2. 2

    ch01 is a relative path, so it's read from the working directory. A new terminal starts in the home folder, and ch01 isn't there; it's inside python-course.

  3. 3

    Two fixes: cd python-course first and then cd ch01, or give the path from home in one go with cd python-course/ch01.

  4. 4

    When a command can't find something, type pwd and ls before anything else. They tell you where you really are and what's there.

    pwd and ls first, guesses second

Check your understanding (counts toward your progress)

You're in python-course/ch01. Go up to python-course, make a folder called ch02, go into it, and check where you are. One line is a decoy.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

cd .. goes up to python-course, so mkdir ch02 makes ch02 beside ch01. cd ~ is the decoy: it would jump all the way home.

Correct answer

Also accepted:

Why

Which command takes you up exactly one level?

Match each command to the question it answers or the job it does.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

pwd and ls only report. mkdir makes a folder without moving you. Every cd changes the working directory: into data, up one level, or home.

Correct answer

Also accepted:

Why

Which commands change the working directory?

Challenge: you're somewhere unknown. Make a folder ch03 inside python-course, which is in your home folder, go into it, and confirm. One line is a decoy.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

cd ~ gives a known starting point, so the relative cd python-course works. Then make ch03, enter it, and check. mkdir ~ makes no sense: your home folder already exists.

Correct answer

Also accepted:

Why

From an unknown place, which command gets you somewhere you know?

Open a new terminal, go into python-course, then into ch01 inside it, and list what's there. One line is a decoy.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

A new terminal starts at home, so cd python-course first, then cd ch01, then ls. mkdir ch01 is the decoy: the folder already exists.

Correct answer

Also accepted:

Why

The folders already exist. Which command would make a new one?

Practice: Opening a terminal and running a command (5 problems · doesn’t affect your progress)

Which command lists the files and folders in the working directory?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

pwd prints where you are, not what's there.

cd moves you.

mkdir makes a folder.

Why

ls answers "what's in here?"

ls

From your home folder: make python-course, go into it, make ch00, go into that, and print where you are.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

Make each folder before entering it, and run pwd last so it reports the final place.

Correct answer

Also accepted:

Why

B → E → D → C → A

You're in python-course/ch01. Which single command takes you back to python-course?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

That goes all the way home.

There's no python-course inside ch01.

mkdir makes folders; it doesn't move you.

Why

cd .. moves up one level, from ch01 to python-course.

cd ..

Sort each item: a Python program, data a program reads, or a command you type in a terminal?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

greet.py and its lines are the program; guests.txt is data it reads; python greet.py and cd ~ are typed at the prompt.

Correct answer

Also accepted:

Why

“greet.py” → Python program; “python greet.py” → Terminal command; “guests.txt, a list of names greet.py reads” → Data; “cd ~” → Terminal command; “print("Hello") inside greet.py” → Python program

Challenge: you type mkdir Python Course (with a space). What do you most likely end up with?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Without quotes, the space splits the name into two words, so mkdir gets two names.

mkdir accepts several names at once and makes each.

The shell doesn't join words; it splits them.

Why

The space makes Python and Course two separate words, and mkdir makes a folder for each. Quote the name, or better, use python-course.

Two folders, Python and Course

Reading output and exit status

A command answers you in two ways. The first is the text it prints, which is its output. Error messages count as output too; they're there for a person to read.

The second answer is quieter. When any program finishes, it hands the computer one whole number, called its exit status (people also say exit code). It works like a thumbs-up or thumbs-down the program gives on its way out. The rule almost every program follows: 0 means "it worked", and any other number means "something went wrong". Which nonzero number came back can hint at what went wrong, and each program documents its own numbers.

You don't see the exit status unless you ask. On a Mac or Linux, typing echo $? right after a command prints its number. ($? is the shell's name for "the last exit status", and echo just prints it.) Windows PowerShell does it a little differently. There, $? prints True or False: did the last command work or not? A second name, $LASTEXITCODE, holds the actual number from the last program you ran.

Python plays by the same rule. A Python program that gets all the way to its end hands back 0. One that stops with an error report hands back 1. Now suppose you ask Python to run a file that isn't in the folder you're standing in. Then Python can't even start your program. It prints a message ending in No such file or directory and hands back 2. Why have a number at all when there's a message? Because other programs read the number. A tool that checks your work looks for 0 before moving on, and the number means the same thing however the message is worded.

A command reports back in two ways. The first is output: text it prints for you to read, including error messages. The second is its exit status (also called an exit code): a whole number every program hands to the operating system when it ends. By a convention nearly every program follows, 0 means success and any other number means some kind of failure; the program's own documentation says what a particular nonzero value means. The exit status isn't printed, so you ask for it. In zsh and bash, echo $? prints the last command's exit status. PowerShell splits the idea in two: $? is True or False for whether the last command succeeded, and $LASTEXITCODE holds the number the last program (rather than a built-in command such as cd) returned.

Python follows the same convention. A program that runs to its last line exits with 0. A program that stops with an error, either kind from the previous section, exits with 1. If you ask Python to run a file that isn't in the working directory, the interpreter itself prints a message ending in [Errno 2] No such file or directory and exits with 2, before reading any of your code. Editors, test runners, and automated build systems all branch on that number rather than on the wording of a message.

In PowerShell, try a command that works, then one that fails, and ask about each one:

cd ~
$?
cd nosuchfolder
$?

The first $? prints True. The second cd prints a red error saying it can't find the path, and the $? after it prints False. Once Python is installed in Chapter 1, $LASTEXITCODE will show the number each Python program handed back.

Try a command that works, then one that fails, and ask about each:

$ cd ~
$ echo $?
0
$ cd nosuchfolder
cd: no such file or directory: nosuchfolder
$ echo $?
1

The first cd succeeded silently, and its exit status is 0. The second printed an error message and returned 1.

Try a command that works, then one that fails, and ask about each:

$ cd ~
$ echo $?
0
$ cd nosuchfolder
bash: cd: nosuchfolder: No such file or directory
$ echo $?
1

The first cd succeeded silently, and its exit status is 0. The second printed an error message and returned 1.

Did it work?

Sort each report: does it say the last command succeeded, or that it failed?

Loading interactive…

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Correct answer

Also accepted:

Why

Why

An exit status of 0 means success; any other number, 1 or 2 alike, means failure. PowerShell's $? turns that into True or False. An empty reply with status 0 is the normal look of a command that worked, and a Python program that ends in an error report has failed (status 1) even though some lines printed first.

Worked example

Silence or failure?

  1. 1

    Sam types mkdir ch02 and gets no reply at all. Did it work?

  2. 2

    No news isn't proof. He types echo $? and sees 0. Success: an empty reply plus status 0 is how a quiet command says "done".

  3. 3

    He types mkdir ch02 a second time. Now there's a message saying the folder already exists, and echo $? prints 1.

    the message is for Sam; the number is for any program watching

  4. 4

    The message says what went wrong in words. The 1 says the same thing as a number, which another program could check without reading the words.

Worked example

A Python program that half worked

  1. 1

    In Chapter 1, Ana runs a four-line program whose third line misspells print. Her terminal shows two printed lines, then Python's error report ending in NameError. She asks for the exit status.

  2. 2

    Two lines appeared, so it's tempting to call it a partial success. But the program stopped before its end because of a runtime error (a problem found while the program was running).

  3. 3

    echo $? prints 1, Python's "stopped with an error" number. In PowerShell, $LASTEXITCODE would hold the same 1.

    printed output before a crash doesn't make it a success

  4. 4

    Any tool watching her program, such as a test runner in a later chapter, sees the 1 and reports a failure, whatever got printed first.

Where this goes next

Chapter 1 §2 has you open this same terminal to install Python. You then type python --version to prove it worked. Chapter 1 §4 runs python hello.py from inside the python-course folder you just made. That works only because hello.py is a relative path, read from the working directory. Chapter 7 §5 gives a project folder its own set of installed tools, switched on from the terminal. Chapter 8 §1 handles paths from inside Python itself. The exit status matters from Chapter 9 §7 on. A test run that fails hands back a nonzero number, and that's how editors and automated checkers know to stop.

Check your understanding (counts toward your progress)

Challenge: you type mkdir ch02 twice in the same folder. What happens the second time?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

One folder can't hold two items with the same name.

The command couldn't do its job, so it reports a failure.

mkdir refuses rather than replacing anything.

Why

The folder already exists, so mkdir can't make it. It prints a message for you and hands back a nonzero status for any program watching.

Can a folder hold two things with exactly the same name?

In PowerShell, $? prints False. What happened?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Success would print True.

$? worked fine; it answered your question.

$? is about the last command, not about any folder.

Why

PowerShell's $? answers "did the last command succeed?" with True or False; $LASTEXITCODE holds a program's actual number.

PowerShell turns success into True and failure into False.

Sort each report: did the last command succeed, or fail?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

0 and True mean success; any other number and False mean failure. A silent command with status 0 worked.

Correct answer

Also accepted:

Why

Only one number means success.

On macOS or Linux, how do you see the last command's exit status?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

pwd prints the working directory.

The exit status isn't printed unless you ask for it.

ls lists files.

Why

echo $? prints the exit status of the command just before it.

$? is the shell's name for the last exit status.

Practice: Reading output and exit status (5 problems · doesn’t affect your progress)

Which exit status means "something went wrong"?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

0 means success.

Any nonzero number, 1 or 2 included, means failure.

Every program hands back a status when it ends.

Why

0 means success; any other number means some kind of failure.

Any number other than 0

In PowerShell, which name holds the actual number the last program handed back?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

In PowerShell $? is only True or False.

pwd prints the working directory.

echo prints things, but it isn't where the number is kept.

Why

PowerShell keeps the last program's exit status in $LASTEXITCODE, and $? says whether the last command succeeded.

$LASTEXITCODE

A Python program reaches its last line with no error. What exit status does it hand back?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

1 is Python's "stopped with an error" status.

The status isn't a count.

2 is what Python returns when it can't open the file at all.

Why

Running to the end without an error is success: exit status 0.

0

Sort each Python run by the exit status it hands back.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

What this task checks

Structure checked automatically. Whether your ideas are convincing isn't graded, so reread with the checklist.

The compiler says

A model answer

Why

A finished run is 0. Stopping with any error, syntax or runtime, is 1. When Python can't open the file, it hands back 2 before reading any code.

Correct answer

Also accepted:

Why

“Printed every line and finished” → 0; “Printed two lines, then a NameError report” → 1; “Printed nothing, then a SyntaxError report” → 1; “Python couldn't find the .py file at all” → 2

Challenge: a test runner reports a failure, but you saw your program print everything you expected. How can both be true?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Exit status is exactly what such tools check.

A program can print plenty, then stop with an error and return 1.

Tools check the number precisely so they don't depend on wording.

Why

Printed output and the exit status are separate reports. A program that prints everything and then stops with an error still hands back a nonzero status.

The program stopped with an error after its output, so its exit status wasn't 0

Feeling good about this section?

Take the section quiz

Every answer gets a full explanation at the end.

Sign in to save highlights

Insert an equation

  • No matches.