Python 3home |
Introduction to Python
davidbpython.com
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 File Explorer or Mac Finder. It is the "power user's" way of talking to the OS, and it should be considered essential for anyone wanting to develop their programming skills. 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 look different (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'). Your prompt may also feature a dollar sign (%) instead of a percent sign. 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 can 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. [Please note: in the paths below you'll see that my class project directory python_data_ipy/ is in my Downloads/ directory (i.e., at /Users/david/Downloads/python_data_ipy). If you want your output and directory moves to match mine, you can put yours there -- or if you can substitute your own directory path for the one I'm using.]
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 or 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 in the below examples, simply substitute the dir command for ls -- also note that Windows output for dir will look different than below):
Davids-MBP-3:Downloads david % pwd /Users/david/Downloads Davids-MBP-3:Downloads david % ls dir on Windows python_data_ipy [... likely other files/folders as well ...] Davids-MBP-3:Downloads david % cd python_data_ipy Davids-MBP-3:python_data_ipy david % pwd /Users/david/Downloads/python_data_ipy Davids-MBP-3:python_data_ipy david % ls dir on Windows 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 username.txt Davids-MBP-3:python_data_ipy david % cd session_06_multis/ Davids-MBP-3:session_06_multis david % ls dir on Windows warmup_exercises inclass_exercises notebooks_inclass_warmup [...several more files and folders, may be in a different order...] Davids-MBP-3:session_06_multis david % cd inclass_exercises Davids-MBP-3:inclass_exercises david % ls dir on Windows inclass_6.1.py inclass_6.2.py inclass_6.3.py inclass_6.4.py inclass_6.5.py inclass_6.6.py ... Davids-MBP-3:inclass_exercises david % pwd /Users/david/Downloads/python_data_ipy/session_06_multis/inclass_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 (Mac paths will of course look different; use pwd to confirm your present working directory):
C:\Users\david> cd Desktop C:\Users\david\Desktop>
But if we'd like to travel up the directory tree, we use the special directory shortcut .. which signifies the parent directory:
C:\Users\david\Desktop> cd .. C:\Users\david\> cd .. C:\Users\>
We can also travel directly to an inner folder by using the full path. In order to complete the next exercise, I'll travel to an inner folder within my project directory (again, yours may be different depending on where you put the project folder):
C:\Users\> cd david\Downloads\python_data_ipy\session_06_multis\inclass_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_ipy\session_06_multis\inclass_exercises\> python inclass_6.1.py
On Mac:
Davids-MBP-3:inclass_exercises david % python3 inclass_6.1.py
Unless you've changed it, you won't see any result from running this program, because it does not print anything. Make a change and run it again to see the result! Each week we'll try to spend a few minutes traveling to and executing one or more Python programs from the command line.