![]() ![]() Here's how I would rewrite that: current_dir = r"C:\Users\User\AppData\Windows\Start Menu\Programs" For instance with python 3, with the command line provided there's an error (but okay in python 2.): Syntaćrror: (unicode error) 'unicodeescape' codec can't decode bytes in position 5-6: truncated \UXXXXXXXX escape If you want to run a command in a specific location, just pass the absolute path of the command (and since it's using string literals, always use r prefix to avoid that some \t or \n chars are interpreted as special characters.). You would have to use os.chdir for that, but you don't even need it. Note that it happens here, as the directory name has spaces in it and isn't quoted.). Current directory doesn't propagate up to the calling process (also, as you're not checking the return code, the command wouldn't fail, even with a non-existing directory. It "works", but in a separate subprocess that exits right away. Although most of the programs that you write in your IDE, you can run directly from there, you may have occasions where you want to run some Python code from the command line and now you know how.Problem is that the system command doesn't work. In order to execute it, I'm going to type python 01_03.py and then hit Enter and we get our Hello world! Throughout this course, I'll be using a Mac computer, so I'll often refer to Python 3 instead but for Windows users, typing strictly Python will work just fine. I'll then type the dir command, which stands for directory to get a list of all the files that are contained in this directory and I see my 01_03.py file. We're now inside of the chapter 01 folder. Now, when we close down the File Explorer, notice that the entire file path has been copied for us. So I'm going to drag this folder over to my command prompt. Then Exercise Files, and I want the file that's inside of chapter 01. I'll choose the Programming Foundations folder. I'm going to open up the File Explorer and then I'll navigate to where our file is located. The first thing we're going to do is to type cd or the change directory command and then we need to find the path of our file. Yours might be in your downloads or another location. My file is located on my desktop inside of the exercise files folder. Next, we need to make sure we're in the same directory as our file. To do that, we'll come down to search and we'll type Command and we'll choose Prompt. First, we'll need to open up the command prompt. ![]() Remember our 01_03.py file? Let's see if we can run the code contained in it. ![]() We can do this by typing exit, open parentheses, close parentheses and then hitting Enter. They need you to follow the syntax or rules in order to understand your commands. Programming languages work much the same way. That's a grammatically correct sentence but if I were to say you're doing a great job with, that would be an incomplete sentence. For example, in English, I could say you're doing a great job with the course so far. But what if we were to just type two plus and then hit Enter? Well, we get another syntax error because our expression was not complete. That's because the expression two plus two is valid in the Python programming language. But this time, we'll type two space plus two and then hit Enter. In this case, we wrote our code in plain English but the interpreter doesn't understand English, only Python. Syntax errors happen when you write code that breaks the expected rules. We call that syntax refers to the rules of a programming language. Well, we get a syntax error from the Python interpreter. Let's see what happens when we type what is two plus two? And hit Enter. We'll start out by trying to get the sum of two plus two. Unlike the Mac, you don't need to type anything else to start sending Python commands. The Python shell will display the prompt, which lets us know that the interpreter is ready for our Python instructions. This is going to open up the Python shell. When you get to the dropdown, choose Python 64 bit. ![]() You can access it on your Windows machine by going to the Start menu and then navigating to the Python folder. I'll be referring to it as the terminal or command line throughout this course. One way to execute or run your Python code is by using the command line. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |