官术网_书友最值得收藏!

Using the terminal to run programs

The Terminal is an interface that allows the user to write instructions directly to the OS. You can use the Terminal to run the Python program that you just created. When running programs in the Terminal, the Terminal will display status and debugging messages produced by the program, which can be very useful.

Each operating system has its own version of the Terminal. To get started, open up the Terminal program for your operating system. In most distributions of Linux it is called Terminal, in Windows it is called Command Prompt, and in macOS it is called shell. Many of the commands also differ between operating systems; however, the commands that we will use in this book will be mostly the same irrespective of operating system. You should see a prompt in the upper left-hand corner that looks something like this:

The first thing that you need to do to run your program is to navigate in the Terminal to the directory containing your program. Terminal commands take place within a specific folder in the computer's filesystem. Windows, Mac, and Linux all use the cd command, which stands for change directory and can be used to change the working directory. To change to the directory containing your hello_world.py file, you can use the following command:

$ cd <relative/path/to/directory>

A couple of things to note here:

When you see angle brackets in a Terminal command or a line of code, this often indicates a placeholder for something that is specific to your system or configuration. In the place of <relative/path/to/directory>, you will need to type the relative path to the folder containing your program (the hello_world.py file that you just created).

The $ symbol indicates that the command should be entered into the Terminal and should not be copied with the rest of the command. Not all Terminal programs use a dollar symbol to indicate a prompt, so don't worry if yours doesn't look exactly like this.

The relative path to the directory refers to the exact sequence of folders leading from the current directory to the destination directory. The current directory is often listed at the beginning of each prompt. I the current directory is not listed at the beginning of the prompt, you can find it by using the pwd command in Linux and Mac.

The following command is used in Linux and Mac in order to show the current working directory:

$ pwd

The following is the command used in Windows to show the current working directory:

$ cd

In the relative path, each subsequent folder is separated by a forward slash (/) in Linux and Mac, and a backward slash (\) in Windows. The following is what it looks like on my computer to find the current directory, change to my chapter2 folder, and then verify that I am in the correct location:

If it is easier, you can also do this incrementally by changing folders one directory at a time. If you accidentally move to the wrong directory, you can use .. to move up one directory in the filesystem:

$ cd .. 

This command will change the working directory to the parent folder of the previous working directory. Another useful command for navigating files is the ls command in Mac and Linux, and the dir command in Windows. This command will list all of the files and folders in the current directory.

In Mac and Linux, the following command is used to list the contents of the current directory:

$ ls

In Windows the following command is used to list the contents of the current directory:

$ dir
主站蜘蛛池模板: 乌拉特中旗| 扬中市| 永平县| 友谊县| 株洲市| 琼结县| 亚东县| 富源县| 宾阳县| 桂阳县| 鹤峰县| 武隆县| 南宫市| 阳高县| 崇仁县| 汤阴县| 章丘市| 望江县| 屏东县| 黔南| 彭阳县| 温宿县| 天津市| 永寿县| 西华县| 汪清县| 习水县| 广汉市| 衢州市| 台南县| 神农架林区| 永修县| 霸州市| 沙坪坝区| 嘉鱼县| 都兰县| 新泰市| 尉氏县| 陇川县| 上饶县| 营山县|