02 The Command Prompt (CMD) and Some Basic Commands | Learn Python like ABC

A complete Python tutorial for anyone who doesn't know anything about programming.

ยท

7 min read

02 The Command Prompt (CMD) and Some Basic Commands | Learn Python like ABC

The Command Prompt

On a Windows machine, the command prompt (also known as CMD) is the command-line interface.

It's basically a text-based interface available to the user.

The user types in inputs (commands) in the form of a text and they are executed.

I know you're probably thinking: but Paul, why would I ever want to go through this stress while Windows has got a nice graphical user interface for me already?

Well, let's just say that when you start programming, you'll end up believing that it makes your life way easier.

And plus, it makes you look cool; you know... ๐Ÿ˜‚

Note: If you're using a macOS or Linux, your terminal is what we'll use in place of the Windows CMD. As you'll see soon, some commands will be similar but most will be different.

Just for information, Linux and macOS both use what we call a bash terminal.

In the last part of this series, I showed you how to open up the command prompt on your windows machine. You just need to go to your start menu and search for CMD.

Meet the Windows Command Prompt! Screenshot (69).png Don't worry if yours doesn't look exactly the same as this one. It should be very similar though

Directories and Paths

Before we start looking at a few commands (the text you'll input into the CMD), let's consider two terminologies.

Directory: A directory is basically a folder. A very simple understanding; don't overthink it. ๐Ÿ˜‚ Understanding it like so makes things clearer.

Path: A path refers to a representation of how one can access a thing on the computer, be it a file or a folder.

For example, if you're on a Windows machine, you'll have to open the C Drive (the drive with the letter C), before you can get access to the folder in which programs on your computer are generally installed (the folder named "Program Files").

A path is usually written with slashes separating the succeeding directories. Whether the slash is a forward or backward slash depends on the system. For Windows command prompt, the backward slash is mostly used.

So a path to the "Program Files" directory we just talked about would look like this: C:\Program Files

Notice that a drive is represented with its letter followed by a colon (:).

Absolute paths start from the drives. So for the path I just provided (C:\Program Files) is an absolute path.

A relative path is one that is given in relation to the current location of the user. The period (.) is used to reference the current location and the path continues from there.

For example, given that I am currently on the desktop and there's a folder on my desktop called "MyFarm", the relative path would be: .\MyFarm.

So the . over there, is representing the Desktop.

Cool?

Some Basic Commands

In relation to the CMD (and terminal), a command is a text that is entered into the CMD in order to tell the computer to do something. (You can get it from the name - Command).

Usually, you press the Return key (Enter key), after entering the command, in order to execute it.

Now let's look at some basic ones.

cls (clear on macOS and Linux)

Most people consider the cls to stand for "clear screen". The command cls, when executed, clears the command prompt. Have a look...

Before: Screenshot (70).png After: Screenshot (71).png

Very simple.

So when you keep typing commands and the window looks clustered, don't hesitate to key in one more to clear it!

The clear command does the same thing in the terminal on macOS and Linux.

cd

This is considered to stand for "Change Directory". It is easy to guess what it does from this point.

The cd command helps the user move from the current directory into an immediate one.

Looking at the picture, I'm currently in a directory called treve with the path (absolute path) C:\Users\treve

I know for a fact that the Desktop is located in this directory.

Let's say I want to use the cd command to change into the Desktop directory, here's what I would do:

I'd enter the command cd Desktop. This command moves into the Desktop directory.

Before: InkedScreenshot (72)_LI.jpg After: InkedScreenshot (73)_LI.jpg

You'll also notice that the path has changed to C:\Users\treve\Desktop, indicating that my current location has been changed into the desktop.

So the format here is that you use the command cd <directory> to move into a directory.

The cd command could also be used with the paths (both relative and absolute) to change the current directory into another but on the same drive. InkedScreenshot (75)_LI.jpg

Going back to the previous directory: In order to move into the previous directory, replace the <directory> in the format given above with .. (Yes, two dots.)

Let's say I'm currently in the Desktop and I'd like to move back to treve, I'll use the command cd ..

Here... InkedScreenshot (76)_LI.jpg

I'd suggest you go through the cd command again. It can be quite tricky to look at, at first.

Using the File Explorer to Open CMD in a Specific Directory

So far we only learned how to open up the CMD from the start menu.

On a Windows machine, you can easily open up the CMD within a current directory using the Windows File Explorer.

Let me show you how.

So I've created a folder for this series: folder.jpg

Given that I wanted to open up this directory in the command prompt, all I need to do is to put my cursor in the box where the directory structure is written as shown below: folder2.jpg Now, I type into this box "cmd" and hit enter: folder3.jpg This should open up the CMD with the folder as the current directory: folder4.jpg

And that's how easy it is to open up the CMD from a specific directory on windows.

dir

Another very useful command to use in the command prompt is the dir.

This basically displays information about the current directory you're in.

It shows the files and folders available in the current directory.

Based on the picture of the folder above, you can see that I have a "downlaods" in there.

Assuming I didn't know and I'd like to find out the folders and files available in the directory, all I need to do is to key in the command dir and hit the Return key (Enter key)... dir.jpg

Switching from one drive to another

Finally, let's learn how you can move from one drive to another.

Well, you'd have to know the letter of the drive you wish to switch to.

The command is simple. Write out the letter followed by a colon: <drive_letter>:.

Example: D: or even d:.

On my computer, I have two drives: C and D. Here's how I switch between them in the command prompt: swtich.jpg You'd also notice that it maintains my location on the D: drive when I switch back to it.

So that's it, in terms of some basic commands.

Using Python in the Command Prompt

Now let's look at how we can use the python interpreter through the command prompt.

It's very simple.

We use the command python and hit enter.

Again, on a macOS and Linux, don't forget to add the specific version of python: python3 python1.jpg

This command should give you a python prompt.

Just like the command prompt, the python prompt allows you to issue python commands.

For now, we're only going to exit it since we've not started anything python yet.

You exit by entering the command exit() and your CMD will be back. python2.jpg

Video Version


Yep, we're done with this one too.

If you like this content and would like to support it, you can do that by simply leaving a LIKE behind.

If you want to become my friend, connect with me on Twitter. The handle is trevenue44.

A video version of this series is on my YouTube channel. Check that out: trevenue44 on YouTube.

I'll see you soon.

ย