Python 3home |
Introduction to Python
davidbpython.com
The Command Prompt includes powerful commands for working with files and programs.
In your Windows search, look for one of the following, and open it:
You should see something similar to the following:
C:\Users\david> < -- command line
After opening this window, note the blinking cursor: this is your computer's operating system, awaiting your next command. (Please note that there may be small differences between your output and this illustration; these can usually be ignored.)
in your Spotlight search, look for Terminal, and open it:
You should see something similar to the following:
Last login: Thu Sep 3 13:46:14 on ttys001 Davids-MBP-3:~ david$ < -- command line
After opening the command prompt program on your computer, note the blinking cursor: this is your computer's operating system awaiting your next command. (Please note that there may be small differences between your output and this illustration; these can usually be ignored.)
Your command line session is located at one particular directory on the file tree at any given time.
On Windows, the pwd is automatically displayed at the prompt:
C:\Users\david>
On Mac/Linux, type pwd and hit [Enter]:
Davids-MBP-3:~ david$ pwd /Users/david
dir is the command to list the contents of a directory.
Type dir and hit [Enter]:
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> .. 08/29/2020 11:28 AM <DIR> Contacts 08/29/2020 12:50 PM <DIR> Desktop ... etc ...
The contents of the directory include all files and folders that can be found in it.
ls is the command to list the contents of a directory.
Type ls and hit [Enter]:
Davids-MBP-3:~ david$ ls Applications Downloads Movies Desktop Dropbox Music Documents Library Public ... etc ...
The contents of the directory include all files and folders that can be found in it.
Starting from the root, each folder may have files and other folders within.
C:\Users
├── david <--- my pwd when I open my Terminal
│ ├── Desktop
│ │ └── python_data
│ │ ├── 00
│ │ ├── 01
│ │ │ ├─ 1.1.py
│ │ │ ├─ 1.2.py
│ │ │ ├─ 1.3.py
│ │ │ etc.
│ │ ├── 02
│ │ │ ├─ 2.1.py
│ │ │ ├─ 2.2.py
│ │ │ ├─ 2.3.py
etc.
cd stands for 'change directory'. This command works for both Windows and Mac.
on Mac/Linux:
Davids-MBP-3:~ david$ pwd /Users/david Davids-MBP-3:~ david$ cd Desktop Davids-MBP-3:~ david$ pwd /Users/david/Desktop
on Windows:
C:\Users\david> cd Desktop C:\Users\david\Desktop>
To visit a directory "below" where we are, we simply name the child dir.
We move "down" the directory tree by using the name of the next directory -- this extends the path:
C:\Users\david> cd Desktop C:\Users\david\Desktop> cd python_data C:\Users\david\Desktop\python_data> cd 02 C:\Users\david\Desktop\python_data\02>
We can also travel multiple levels by specifying a longer path:
C:\Users\david> cd Desktop\python_data\02 C:\Users\david\Desktop\python_data\02>
(Please that on Windows we use the backslash separator (\); on Mac it is the forward slash(/).)
The '..' (double dot) indicates the parent directory and can move us one directory "up".
If we'd like to travel up the directory tree, we use the special .. directory value, which signifies the parent directory:
C:\Users\david\Desktop\python_data\02> cd .. C:\Users\david\Desktop\python_data> cd .. C:\Users\david\Desktop> cd .. C:\Users\david>
We can also travel multiple levels with multiple ../'s:
C:\Users\david\Desktop\python_data\02> cd ..\..\.. C:\Users\david>
(Please that on Windows we use the backslash separator (\); on Mac it is the forward slash(/).)
These two commands together allow us explore our filesystem.
Here is an example journey through some folders, viewing the contents of each folder as we move (this shows Mac/Linux output, but in Windows you may replace ls with dir):
Davids-MBP-3:Desktop david$ pwd /Users/david/Desktop Davids-MBP-3:Desktop david$ ls python_data Davids-MBP-3:Desktop david$ cd python_data Davids-MBP-3:python_data david$ pwd /Users/david/Desktop/python_data Davids-MBP-3:python_data david$ ls 00 01 02 ... etc. Davids-MBP-3:python_data david$ cd 02 Davids-MBP-3:02 david$ ls 2.1.py 2.2.py 2.3.py 2.4.py 2.5.py 2.6.py 2.7.py 2.8.py 2.9.py 2.10.py Davids-MBP-3:02 david$ pwd /Users/david/Desktop/python_data/02