Open In App

How To Save Python Scripts In Linux Via The Terminal?

Last Updated : 28 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Linux users like to do tasks using the terminal as it gives the user more power to do anything with the system whether it is to store, create, or remove files. Other than file management and system control tasks, the terminal is used to write scripts for any programming language. Python and Bash scripts are mostly typed using a terminal and it is best practice to do so, as it makes the programmer more versatile.

In this article, we are going to look into how can we write and save Python scripts using a terminal with terminal editors like Vim and Nano.

Using Vim Editor to Save Python Script in Linux

Step 1: Create and open the file using Vim

We are going to create a file and start writing our Python script using Vim. For that, open your terminal and write the following command.

vim script.py

The above command will open vim editor with the file name “script.py” and then we can start writing script in the file.

Screenshot-from-2024-02-01-20-17-19

Starting vim editor

Step 2: Vim and python script

Vim editor has two modes:

Command mode: This mode is used for running commands to save file, undo changes and many more. In this article we are going to see the general commands that are used to save files. You can search for vim cheatsheet as vim has a lot of commands.

Insert mode: In this mode, we write the stuff we want to. All writing related stuff is done in Insert mode.

How to switch between these two modes?. Well it is quite simple, for that you have to check, if you are in insert mode or command mode.

  • esc: press escape key to switch to command mode
Screenshot-from-2024-02-01-20-42-49

Command mode

  • i : Press “i” to switch into insert mode
Screenshot-from-2024-02-01-20-43-24

insert Mode

The above picture explains the mode in which you are. If it says “Insert” You are in insert mode and write your stuff.

Step 3: Writing script and saving the script

before writing make sure to switch to insert mode by pressing “i” key on your keyboard. Now lets write a small script and save it.

Screenshot-from-2024-02-01-20-49-43

Python Script

After writing our script, it’s time to save the script. For that make sure to switch to “Command mode” by pressing “esc” key.

commands to save file:

  • :wq – This command will save the file and quit the vim editor
  • :w –This command will save or write the file but didn’t quit the vim editor
  • :q -this command will simply quite the vim and your work will go in vain.
Screenshot-from-2024-02-01-20-58-34

Saving the file

After entering the command press enter and then your script will be saved in your present working directory.

To run the python script simply run the following command:

python3 script.py

Output:

Screenshot-from-2024-02-01-21-04-50

Output

Using Nano Editor to Save Python Script in Linux

Step 1: Starting nano editor

To create file and starting writing python script, it follows the same command as vim but here we use “nano” instead of “vim”.

Command:

nano script.py

Output:

Screenshot-from-2024-02-01-21-08-34

nano interface

Step 2: Writing and Saving the script

Commands to save the script in nano. There are various commands in nano, but we are only interested in saving the script. If you want to know the commands of nano check the cheatsheet for it.

Commands:

  • Ctrl + o -This command will save the script and any other stuff in the file but it will not exit the nano editor, hit enter after pressing the keys
  • Ctrl + x –This will exit the nano editor, use this after saving the file successfully

Lets write a script and then save it using nano.

print(0)
if (0): 
    print("GFG") 
else: 
    print("Like this article.")
Screenshot-from-2024-02-01-23-18-19

Script

Save the script: Press “Ctrl + o ” and hit “Enter” to save and then press “ctrl + x” to exit the nano editor.

Step 3: Execute the script

Executing the script is same that is :

Command:

python3 script.py

Output:

Screenshot-from-2024-02-01-23-26-59

Output

How To Save Python Scripts In Linux Via The Terminal – FAQs

How do I create a new Python script file in Linux using the terminal?

To create a new Python script file, you can use a text editor like Nano or Vim. Simply open the terminal and type `nano filename.py` or `vim filename.py`, then start writing your Python code. Save the file by pressing `Ctrl + X` followed by Y in Nano, or by typing `:wq` and pressing `Enter` in Vim.

Can I save a Python script with a different name or extension in Linux?

Yes, you can save your Python script with any name and extension you prefer. The convention is to use the `.py` extension for Python scripts, but it’s not mandatory. You can save it with any name and extension, but ensure that you execute it accordingly.

How do I save changes to an existing Python script in Linux via the terminal?

To save changes to an existing Python script, open the script file in a text editor using commands like `nano filename.py` or `vim filename.py`. Make your changes, then save the file by following the instructions provided by the text editor (e.g., `Ctrl + X` followed by `Y` in Nano, or `:wq` in Vim).

Is there a way to automate the saving of Python scripts in Linux via the terminal?

Yes, you can automate the process of saving Python scripts using terminal commands. One common way is by using shell scripting. You can create a shell script file (.sh) that contains commands to create or edit Python scripts and then execute them. Remember to make the shell script executable using the `chmod +x script.sh` command.

What are some best practices for organizing and saving Python scripts in Linux?

It’s a good practice to organize your Python scripts in a dedicated directory. You can create a directory for your Python projects and save your scripts there. Additionally, consider using version control systems like Git to keep track of changes and collaborate with others effectively. Furthermore, adhere to naming conventions and use meaningful names for your script files to improve readability and maintainability.

Conclusion

In this article we discussed how to use the terminal in Linux to write and save Python scripts using Vim and Nano editors. It explains basic commands and steps for creating, editing, and saving scripts. Additionally, it provides answers to common questions, suggesting best practices for organizing scripts and using version control. By mastering these techniques, users can efficiently manage Python projects in Linux.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads