Command Line Tutorial

This brief tutorial introduces the command-line skills used in Python Foundations. It is not a general Terminal or Command Prompt course. Allow about 10 minutes and complete only the section for your operating system.

Before entering commands

A command prompt is a place where you give the computer text instructions. Type one command and press Return on macOS or Enter on Windows.

The prompt displays information about your current session. Examples in tutorials often show a prompt character such as $, %, or >. Do not type the prompt itself. Type only the command that follows it.

Current directory and directory structure

The current directory is an abstraction that tells the operating system, command shell, or Python interpreter where to start looking for files. A filename such as measurements.csv usually means “the file named measurements.csv in the current directory.”

A directory structure is another abstraction. It allows us to organize and work with files as though they were kept in nested physical folders. A path describes how to move through the structure to reach a directory or file. Paths consist of a series of directory (or folder) names separated by a character such as / or \. The beginning of a path is considered the "top" or "root", and subsequent directory names are considered to be moving "down" the structure. On macOS, an absolute or "full" path begins with /. On Windows, a full local path usually begins with a drive designation such as C:\. A relative path does not begin at the root and is considered to start in the current directory.

In Windows, storage is often divided into "drives" identified by letters (calling it a drive hearkens back to the days when files were stored on magnetic tapes or spinning disks that had a mechanical drive to keep them moving and control where reading and writing was happening). The primary local drive is commonly C:\. Network storage may be mapped to another drive letter by your IT department, or it may be accessed through a network path such as \\server\share.

Home directory

Each user account has a home directory: the user’s default location in the file system. It commonly contains directories such as Desktop, Documents, and Downloads. On macOS, a tilde (~) is shorthand for your home directory. On Windows, the home directory is commonly located under C:\Users\your-name, although organizational settings such as OneDrive can affect the locations of Documents and other directories.

macOS: Terminal

  1. Open Spotlight Search by pressing +Space.
  2. Type Terminal, then press Return.
  3. At the prompt, enter each command below one at a time.
pwd
ls
mkdir python-command-line-practice
cd python-command-line-practice
pwd
ls
cd ..
pwd

What those commands do

pwdprint working directory
Displays the path of your current directory—your present location in the file system. This is where the operating system or Python interpreter starts looking when you provide a relative path.
lslist
Lists the files and directories in your current location. The new practice directory is empty, so the second ls may display nothing.
mkdirmake directory
Creates a new directory named python-command-line-practice.
cdchange directory
Changes your current directory to the new practice directory.
cd ..
Returns to the parent directory—the directory one level above your current location.

Windows: Command Prompt

  1. Open the Start menu, type Command Prompt, and open the Command Prompt application. Alternatively, press ⊞ Win+R, type cmd, and press Enter.
  2. At the prompt, enter each command below one at a time.
cd
dir
mkdir python-command-line-practice
cd python-command-line-practice
cd
dir
cd ..
cd

What those commands do

cdcurrent directory or change directory
When used by itself in Windows Command Prompt, displays the path of your current directory. When followed by a directory name or path, it changes the current directory.
dirdirectory
Lists the files and directories in your current location. The new practice directory is empty, so the second dir may report that it contains no files.
mkdirmake directory
Creates a new directory named python-command-line-practice.
cdchange directory
Changes your current directory to the new practice directory.
cd ..
Returns to the parent directory—the directory one level above your current location.

Two useful habits

  • Use Tab completion. Type the first few characters of a directory or filename and press Tab. The command prompt may complete the name for you. This reduces typing errors. If nothing happens, check your spelling and current directory. There may also be more than one matching name; pressing Tab again may display or cycle through the choices.
  • Check where you are. If a command cannot find a file, use pwd on macOS or cd on Windows, then list the directory contents again.
If you see an error

Read the entire message. Check the spelling of the command and directory name, including hyphens and spaces. When asking for help, copy the prompt, the command you entered, and the complete error message.

If mkdir reports that the directory already exists, continue with the next command.

Quick check

Without looking at the explanations above, can you answer these questions?

  • How do you display your current location?
  • How do you list the contents of the current directory?
  • How do you enter a directory named examples?
  • How do you return to the parent directory?
Show the answers
  • Use pwd on macOS or cd in Windows Command Prompt to display your current location.
  • Use ls on macOS or dir on Windows to list the current directory.
  • Use cd examples to enter the examples directory.
  • Use cd .. to return to the parent directory.

You are finished when:

You can complete the quick check and understand that commands act relative to your current directory. Memorization is not required; the goal is to recognize the commands and follow directory-navigation instructions during class.

Return to the Python Foundations readiness check  |  View Python Foundations prerequisites