Python 001: Command Line Startup

This will help you:

Use the terminal/command line to navigate files and run Python programs.

The terminal or command line is something you may have seen before, but if you've never used it, it could seem advanced or intimidating. It's true that the command line has a lot of power over your computer, and there are many commands you can learn to use. However, if you just learn a few commands, you can make use of the command line pretty easily. It can be used to download files, navigate or modify the files and folders on your computer, run programs, and a lot more - it's basically a direct interface with your computer's operating system.

Lucky for you, there are a few actions which are most useful for running Python programs, so this will just explain those.

Time: .5 - 1 hour / Level: A0

You should already:

  • Be ready to learn!

The Terminal/Command Line

To run your programs, you're going to need to use a command line interface. This just means a program where you can type commands, and they will tell your computer to do something. You probably already have at least one command line interface/terminal program on your computer. Windows has a program called Command Prompt, but there is also PowerShell, which I strongly prefer and recommend because it uses, for the most part, the same command language as command line interfaces on Linux machines. Why do you care? Because a lot of programming resources and tutorials assume you're working on a Linux machine, because it's what many programmers use, so being used to the Linux style of commands will make it easier for you to work with new, unfamiliar tools.

So, either find PowerShell on your computer or install it. This page describes how to do both. An excerpt from the page, on finding PowerShell:

  • Windows 10: Click left lower corner Windows icon, start typing PowerShell

  • Windows 8.1, 8.0: On the start screen, start typing PowerShell. If on desktop, click left lower corner Windows icon, start typing PowerShell

  • Windows 7 SP1: Click left lower corner Windows icon, on the search box start typing PowerShell

Once you open it, you'll see a window with a little bit of text. The first bit of text is copyright information for PowerShell. The next bit of text is a directory location ending with a >. It might look something like this: PS C:\Users\firstlast>.

The File Tree

The directory it opened to is your home directory. It's your default starting point, but it's not the starting point of all directories.

Hold on, what am I talking about with directories? Maybe none of this means anything to you. Let me explain:

When you open a folder on your computer (like Documents or Downloads), those are directories in the file tree or file system of your computer. The file system is the organized storage system for your computer's memory, including files and programs that you download or create. It can be called a file tree because the way it is organized is analogous to a tree. The tree is made up of all the directories (folders and files) stored in your computer. Just like branches can have other branches coming off of them, directories can have subdirectories within them. Those subdirectories are themselves directories, which may contain further subdirectories (like smaller branches) or files (which are kind of like leaves). Going in reverse, a certain directory in most cases has a parent directory which contains it, just like branches an leaves originate from other branches.

The tree analogy works because you can't have directories that create loops (a branch can't sprout from one of its own child branches) and each file or directory can only be contained by one immediate parent directory (a leaf or branch can't be growing from 2 different branches.)

So, for example, if your home directory is C:\Users\firstlast, that is a directory whose parent directory is C:\Users (the firstlast branch is growing from the Users branch). The parent of C:\Users is C:\, which is called the root directory. All your files are children or descendants of the root directory (all branches of the tree grow from the root.)

Your home directory likely has child directories, like Downloads, Documents, etc. which are branches off of the home directory and may themselves contain files and child directories.

Navigating Directories

When you open the terminal, you are automatically in your home directory. In other words, the home directory is your current directory or working directory. Any command you execute will be executed relative to that position in the file tree. For example, try the command pwd (for print working directory):

$ pwd

IMPORTANT: When you see a snippet of code starting with a $, that means that it is a command that you should type in the command line. The $ is a stand in for the prompt which ends with a >, where you can type commands. So, if you see code with a $, you should ignore the $ and type what follows it in the terminal, after the >. Hitting Enter/Return will execute the command.

If you type in pwd and hit Enter, you should see the address of the current directory printed. It tells you where you are in the file tree. Let's try another one, ls:

$ ls

ls stands for list subdirectories, and it tells you what the current directory contains. It will be a combination of file names and folder (directory) names, depending on the directory you're in.

If you're in one directory and need to get to another one, how do you change directories? You use the cd command for change directorycd is used by typing cd followed by either a relative path or an absolute/full path. A relative path is a path (sequence of directory names) that leads to a certain directory, but it assumes that the starting point is the current directory. Here's an example with the following file structure:

  • C:

    • Users

      • firstlast

        • Documents

          • myfolder1

          • myfolder2

        • Downloads

C:\Users\firstlast> pwd
  C:\Users\firstlast

C:\Users\firstlast> ls
  Documents
  Downloads

C:\Users\firstlast> cd Documents

C:\Users\firstlast\Documents> pwd
  C:\Users\firstlast\Documents

C:\Users\firstlast\Documents> ls
  myfolder1
  myfolder2
  myfile1.txt
  mypic1.jpg

C:\Users\firstlast\Documents> cd myfolder1

C:\Users\firstlast\Documents\myfolder1> cd ..

C:\Users\firstlast\Documents> cd ..

C:\Users\firstlast> cd Downloads

C:\Users\firstlast\Downloads>

When you are in the firstlast directory, you can use cd Documents because Documents is the relative path of Documents from firstlast. Once in Documents, you can type cd myfolder1 to get to myfolder1.

Another thing that's useful is the path name .., which means "the parent directory". So, cd .. moves out of the current directory to its parent - like climbing down from a far-out branch on a tree. You can actually use it as part of a longer pathname. For example, instead of typing

C:\Users\firstlast\Documents\myfolder1> cd ..

C:\Users\firstlast\Documents> cd ..

C:\Users\firstlast>

You could have simply used

C:\Users\firstlast\Documents\myfolder1> cd ../..

C:\Users\firstlast>

Or, instead of using

C:\Users\firstlast\Documents> cd ..

C:\Users\firstlast> cd Downloads

C:\Users\firstlast\Downloads>

To get to a sibling folder, you could have just used

C:\Users\firstlast\Documents> cd ../Downloads

C:\Users\firstlast\Downloads>

The \ or / characters separate directory names in PowerShell's command language. In Linux, only / is used to separate directories.

To get to myfolder1 from Downloads, you can't just use cd myfolder1, because myfolder1 is the relative path assuming you're in Documents currently. Instead you would use:

C:\Users\firstlast\Downloads> cd ../Documents/myfolder1

C:\Users\firstlast\Documents\myfolder1>

Absolute Paths

We talked about relative paths a bit, but didn't explain absolute paths. Absolute paths specify exactly where a directory is, independent of your current directory. The command line knows that a path name is absolute because it includes the root (very beginning) directory. C:\Users\firstlast\DownloadsC:\, and C:\Users\firstlast\Documents\myfolder1 are all absolute paths. C:\ is the root directory.

There are a couple of common shortcuts for absolute paths. A single / or \, preceded by nothing, indicates the root directory. So, no matter what directory you're in, this will work:

C:\any\directory\path\at\all> cd /

C:\>

cd \ will work as well for PowerShell. Like .., you can use the \ as part of longer path names, as long as whatever follows it is a path starting at the root directory. Basically, you can write cd /Users instead of cd C:/Users.

This is more useful to know because it can be confusing to know where to put / in path names, and you should know that putting it at the beginning will tell the terminal that it's a path from the root directory.

Another shortcut is ~, which represents the path of your home directory.

C:\any\directory\path\at\all> cd ~

C:\Users\firstlast>

It's very handy because a lot of the files you work with may be in your home directory, so having a shortcut for the absolute path of your home directory can make it easier to give the absolute path of arbitrary files.

C:\any\directory\path\at\all> cd ~/Python/project_1

C:\Users\firstlast\Python\project_1>

One last thing: . (single, not double dots) is the current directory. You won't use this too often, since the current directory is implicit, but it is needed to run executable files (commands or programs) that are in the current directory.

Exercise: Project Folder

For any activity in this collection, you'll want to have all the files downloaded, ideally with the same folder organization as on the site. This means if there are subfolders, it will make the activity easier to leave subfolder contents where they are, and similarly not move files that are in the main folder of the activity.

As an exercise, follow these directions to make a folder for coding projects in your home directory, add an activity folder to it, and run a Python program in it.

$ cd ~
$ mkdir PythonProjects # or whatever you want to call it

mkdir, for make directory, makes a folder with the provided name in the current directory. Download an activity folder, unzip it, and move the whole folder into your projects folder.

$ mv ~/Downloads/activity_folder ~/PythonProjects

mv, for move, moves the first directory into the second directory. Remember that: the first one goes inside the second one. Another useful command is moving all the files from one directory into another:

$ mv ~/PythonProjects/activity_folder/* ~/PythonProjects/activity_copy

/* will match any file or directory name under the previous path name, since * is a wildcard that matches any sequence of characters. Anything that matches that pattern will be moved, so all the contents of activity_folder will be moved.

Finally, you have a directory for all your coding activities, and you have at least one activity downloaded. Now, let's say you wanted to work on that activity, starting fresh. You'd open a terminal window, and then navigate to the activity folder:

C:\Users\firstlast> cd PythonProjects/activity_folder

C:\Users\firstlast\PythonProjects\activity_folder>

Then, you could see what's in the folder:

C:\Users\firstlast\PythonProjects\activity_folder> ls
  README.md
  hello.py
  test.py
  example.py

Finally, to run a Python program:

C:\Users\firstlast\PythonProjects\activity_folder> python hello.py
  Hello World!

C:\Users\firstlast\PythonProjects\activity_folder>

If you modified hello.py, you could make something different happen. Try setting up one of the activities this way, and practice navigating the file system and running programs. Have fun!