Python 3home |
The Command Line (also known as "Command Prompt" or "Terminal Prompt") gives us access to the Operating System's files and programs.
Before the graphical user interface was invented, programmers used a text-based interface called the command line to run programs and read and write files. Programmers still make heavy use of the command line because it provides a much more efficient way to communicate with the operating system than Windows Explorer or Mac Finder. It is the "power user's" way of talking to the OS. To reach the command line, you must search for and open one of these programs:
On Windows -- search for Command Prompt:
Microsoft Windows [Version 10.0.18363.1016] # these 2 lines may not appear (c) 2019 Microsoft Corporation. All rights reserved. C:\Users\david> < -- command line
On Mac -- search for Terminal:
Last login: Thu Sep 3 13:46:14 on ttys001 Davids-MBP-3:~ david$ < -- command line
Your command line will look similar to those shown above, but will have different names and directory paths (for example, your username instead of 'david'). After opening the command line program on your computer, note the blinking cursor: this is the OS awaiting your next command.
Your command line session works from one directory location at a time.
When you first launch the command line program, you are placed at a specific directory within your filesystem. We call this the "present working directory". You may "move around" the system, and when you do, your pwd will change. By default, your initial pwd is your home directory -- the directory at which all your individual files are stored. This directory is usually named after your username, and can be found at /Users/[username] or C:\Users\[username]. On Windows: Your present working directory is always displayed as the command prompt.
C:\Users\david>
On Mac:
Your present working directory can be shown by using the pwd command:
Davids-MBP-3:~ david$ pwd /Users/david
As we move around the filesystem, we will see the present working directory change. You must always be mindful of the pwd as it is your current location and it will affect how you access other files and programs in the filesystem.
We can list out the contents (files and folders) of any directory.
On Mac, use the 'ls' command to see the files and folders in the present working directory:
Davids-MBP-3:~ david$ ls Applications Desktop Documents Downloads Dropbox Library Movies Music Public PycharmProjects Sites archive ascii_test.py requests_demo.py static.zip
On Windows, use the 'dir' command to see the files and folders in the present working directory:
C:\Users\david> dir Volume Serial Number is 0246-9FF7 Directory of C:\Users\david 08/29/2020 11:37 AM <DIR> . 08/29/2020 11:37 AM <DIR> .. 05/29/2020 06:27 PM <DIR> .astropy 05/29/2020 06:35 PM <DIR> .config 05/29/2020 06:36 PM <DIR> .matplotlib 08/07/2020 10:33 AM 1,460 .python_history 08/29/2020 11:28 AM <DIR> 3D Objects 08/29/2020 11:28 AM <DIR> Contacts 08/29/2020 12:50 PM <DIR> Desktop 08/29/2020 11:28 AM <DIR> Documents 09/02/2020 10:25 AM <DIR> Downloads 08/29/2020 11:28 AM <DIR> Favorites 08/29/2020 11:28 AM <DIR> Links 08/29/2020 11:28 AM <DIR> Music 08/29/2020 11:29 AM <DIR> OneDrive 08/29/2020 11:28 AM <DIR> Pictures 08/29/2020 12:46 PM <DIR> PycharmProjects 08/29/2020 11:28 AM <DIR> Saved Games 08/29/2020 11:28 AM <DIR> Searches 08/29/2020 11:28 AM <DIR> Videos 1 File(s) 1,460 bytes 20 Dir(s) 7,049,539,584 bytes free
The 'change directory' command moves us 'up' or 'down' the tree.
To move around the filesystem (i.e. to change the present working directory), we use the cd ("change directory") command. In the examples below, note how the present working directory changes after we move.
on Mac:
Davids-MBP-3:~ david$ pwd /Users/david Davids-MBP-3:~ david$ cd Downloads Davids-MBP-3:~ david$ pwd /Users/david/Downloads
on Windows:
C:\Users\david> cd Downloads C:\Users\david\Downloads>
So using the ls/dir command together with the cd command, we can travel from directory to directory, listing out the contents of each directory to decide where to go next (for Windows, simply substitute the dir command for ls:
Davids-MBP-3:Downloads david$ pwd /Users/david/Downloads Davids-MBP-3:Downloads david$ ls python_data Davids-MBP-3:Downloads david$ cd python_data Davids-MBP-3:python_data david$ pwd /Users/david/Downloads/python_data Davids-MBP-3:python_data david$ ls all_files session_00_test_project session_01_objects_types session_02_funcs_condits_methods session_03_strings_lists_files session_04_containers_lists_sets session_05_dictionaries session_06_multis session_07_functions_power_tools session_08_files_dirs_stdout session_09_funcs_modules session_10_classes Davids-MBP-3:python_data david$ cd session_02_funcs_condits_methods/ Davids-MBP-3:session_02_funcs_condits_methods david$ ls warmup_exercises inclass_exercises notebooks_inclass_warmup python.png Davids-MBP-3:session_02_funcs_condits_methods david$ cd warmup_exercises/ Davids-MBP-3:warmup_exercises david$ ls warmup_2.1.py warmup_2.2.py warmup_2.7.py warmup_2.10.py warmup_2.3.py warmup_2.8.py warmup_2.11.py warmup_2.4.py warmup_2.9.py warmup_2.12.py warmup_2.5.py warmup_2.13.py warmup_2.6.py Davids-MBP-3:warmup_exercises david$ pwd /Users/david/Downloads/python_data/session_02_funcs_condits_methods/warmup_exercises
The '..' (double dot) indicates the parent and can move us one directory "up".
As you saw, we can move "down" the directory tree by using the name of the next directory -- this extends the path:
C:\Users\david> cd Downloads C:\Users\david\Downloads> cd python_data C:\Users\david\Downloads\python_data> cd session_02_funcs_condits_methods C:\Users\david\Downloads\python_data\session_02_funcs_condits_methods>
But if we'd like to travel up the directory tree, we use the special .. which signifies the parent directory:
C:\Users\david\Downloads\python_data\session_02_funcs_condits_methods> cd .. C:\Users\david\Downloads\python_data> cd .. C:\Users\david\Downloads> cd .. C:\Users\david>
We can also travel directly to an inner folder by using the full path:
C:\Users\david> cd python_data\session_02_funcs_condits_methods\warmup_exercises C:\Users\david\Downloads\python_data\session_02_funcs_condits_methods\warmup_exercises>
This is the "true" way to ask Python to execute our script.
Every developer should be able execute scripts through the command line, without having to use an IDE like PyCharm or Jupyter. If you are in the same directory as the script, you can execute a program by running Python and telling Python the name of the script:
On Windows:
C:\Users\david\Downloads\python_data\session_02_funcs_condits_methods> python warmup_2.1.py
On Mac:
Davids-MBP-3:warmup_exercises david$ python warmup_2.1.py
Each week we'll try to spend a few minutes traveling to and executing one or more Python programs from the command line.