This is a very useful practice for keeping logs. Every time it is executed, the output is recorded in a text file. Let's say we also want to write the output of this script to another file. In the examples we have seen so far, the output of the file is displayed in the terminal. Great! We have managed to execute a Python script with a command-line argument. Let's ask the script to find the date 10 days from now: Once you write the Python code above in the file "from_today.py", exit the edit mode and save it. To use command-line arguments, we need the sys module. The next step is to open the Python file in the terminal using the command "vim from_today.py" and write the script that performs the task. The value of n is determined using a command-line argument. Let's write another script that gives us the date that is n number of days from today. Python scripts may be executed with arguments. We type that along with the name of the file to be executed.Īs we see in the output above, our script prints today's date in the terminal. The command to execute a Python file is "python" or "python3" depending on how Python is installed on your computer. We open a terminal and change the directory to "project-1" since the "today.py" file was saved in that directory. How about executing the file to see what it does? We can run Python files in the terminal as well.įirst, we need to make sure the current working directory is the one in which the file is located. We have learned how to create Python files and how to open Python files in the terminal to view their contents. Then, you see the following screen: How to Run Python Files in the Terminal Any other text editor, such as Sublime and Atom, works similarly. Type the command "vim today.py" in the terminal and hit enter. Since the text editor knows the file exists, it opens the file instead of creating a new one. This command is the same as the one used for creating the file. The command "vim today.py" opens this Python file in the terminal. Let's see an example using the file we just created. We can view the content of existing files with a text editor. The file "today.py" has been saved in the "project-1" directory. The editor closes, and you see the following screen. Once you type the ":wq" command, hit enter. The "w" is for "write", meaning we are writing to the file "today.py". The command to save the file and close the editor is ":wq". Do not close the terminal yet because the file has not been saved. Finally, we print today's date using an f-string. The str constructor converts the date object to a string. Since we only need the date, we may also use the date method. The today method of the datetime module returns a timestamp that contains the current date and time. Here is a list of top Python libraries if you'd like to discover more. Python also has numerous third-party libraries created by an active open-source community. Modules and libraries make development processes easier because we can import and use their functionalities right away. In this first line, we import the datetime module. There are many different ways of doing this simple task here, we use the built-in datetime module of Python. As its name suggests, it prints today's date when executed. This Python file performs a very simple task. When you hit "i", you see the following screen, and you can insert text into the file. To modify it, we need to hit "i", the command for switching to the insert or edit mode. Make sure it ends with the extension ".py".Īfter typing this command, hit enter. My Python file is called "today.py." You may name yours anything you'd like. We create a Python file by typing "vim" along with the file name. Then, we change the working directory to the project folder using the "cd" command. Let's start with opening a terminal and creating a project directory. How to Create Python Files in the Terminal Vim comes pre-installed on macOS, but there are many different alternatives to choose from such as Sublime and Atom. We will use the text editor Vim to create a Python file. Let's go back to our main topic of working with Python files in the terminal. By completing this course, you learn concepts such as variables, lists, conditional statements, loops, and functions, which are fundamental topics for programming and software development. If you are new to Python, start learning with Python Basics. Furthermore, the interactive console of allows for practicing, a must in learning a programming language. It offers several well-organized courses and tracks. takes it one step further and makes the Python learning experience smooth and efficient. Thankfully, Python is an easy-to-learn language that is popular for its simplicity. However, how to write Python code or scripts is a separate topic.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |