Creating a Python file in a Linux terminal is quite straightforward and requires a basic understanding of Linux terminal commands. In this short tutorial, we will teach you how to create a Python (.py) file using the Linux terminal, step by step.
Step 1: Open the Terminal
Firstly, you need to open the terminal. This can typically be done by searching for “terminal” in your applications menu or by using the keyboard shortcut Ctrl+Alt+T.
Step 2: Navigate to the Desired Directory
After opening the terminal, you need to navigate to the directory where you want to create the Python file. For this, use the “cd” (change directory) command followed by the path of the directory.
1 |
cd /path/to/directory |
Step 3: Create a Python File
Once you’re in the desired directory, you can create a new Python file using the “touch” command followed by the name of the file (including the .py extension). Below is an example of creating a file named “example.py”.
1 |
touch example.py |
Before Conclusion:
Let’s sum up the commands:
1 2 |
cd /path/to/directory touch example.py |
Step 4: Edit the Python file
Now, open the Python file you have just created using any text editor of your choice. We will use the nano editor in this guide. To open the file, use the “nano” command followed by the name of the file.
1 |
nano example.py |
You can now write your Python code as usual.
Step 5: Save and Exit
Once you’ve finished writing your Python code, you can save and exit the file. In Nano, this can be done by pressing Ctrl+X, then pressing Y to confirm that you want to save the changes, and finally pressing Enter to confirm the file name.
Code:
1 2 3 |
cd /path/to/directory touch example.py nano example.py |
Conclusion
Creating a Python file in the Linux terminal can be done in a few easy steps. After mastering this technique, you will find it very useful especially when dealing with remote servers where a graphical user interface is not available. Always remember to save your progress while editing the file to prevent any loss of code.