How To Save Python Scripts In Linux Via The Terminal?
Last Updated :
28 Feb, 2024
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.
Starting vim editorStep 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
Command mode- i : Press "i" to switch into insert mode
insert ModeThe 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.
Python ScriptAfter 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.
Saving the fileAfter 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:
OutputUsing 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:
nano interfaceStep 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.")
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:
OutputConclusion
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.
Similar Reads
How to Exit a Python Program in the Terminal
Exiting a Python program might seem like a straightforward task, but there are several ways to do it depending on the context and requirements of your script. Whether you want to gracefully shut down your application or terminate it immediately, knowing the right approach can help you manage your co
3 min read
How to Save Seaborn Plot to a File in Python?
Seaborn provides a way to store the final output in different desired file formats like .png, .pdf, .tiff, .eps, etc. Let us see how to save the output graph to a specific file format. Saving a Seaborn Plot to a File in Python Import the inbuilt penguins dataset from seaborn package using the inbuil
2 min read
Save Image To File in Python using Tkinter
Saving an uploaded image to a local directory using Tkinter combines the graphical user interface capabilities of Tkinter with the functionality of handling and storing images in Python. In this article, we will explore the steps involved in achieving this task, leveraging Tkinter's GUI features to
4 min read
Bash Scripting - How to Run Bash Scripting in Terminal
In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we prin
2 min read
How to Install Python-sh on Linux?
In python, the sh package is a full-fledged sub-process replacement for Python 2.6 - 3.8, PyPy, and PyPy3 that allows you to call any program as if it were a function. So, in this article, we will be installing the sh package in Python on Linux operating system. Installing Sh package on Linux using
1 min read
How to take screenshot using Selenium in Python ?
Selenium offers a lot of features and one of the important and useful feature is of taking a screenshot. In order to take a screenshot of webpage save_screenshot() method is used. save_screenshot method allows user to save the webpage as a png file. Syntax : driver.save_screenshot("image.png") Argum
1 min read
Open and Run Python Files in the Terminal
The Linux terminal offers a powerful environment for working with Python files, providing developers with efficient ways to open, edit, and run Python scripts directly from the command line. Open and Run Python Files in the Linux TerminalIn this article, we'll explore various techniques and commands
2 min read
How to setup Notepad to run Python Script
Due to its simplicity, versatility, and readability, Python has become phenomenally popular as a computer language that is highly leveled and dynamically typed. These are some of the IDEs for Python development that have many tools; however, if you are just starting or want it lighter, then Windowsâ
2 min read
How To Run Bash Script In Linux?
Bash scripts, also known as shell scripts, are powerful tools in the world of command-line automation. They allow you to perform a series of tasks or execute commands by writing scripts that can be run in a terminal or command-line interface. However, the question often arises: how do you run a Bash
6 min read
How to Install Python-pickle package on Linux?
Python pickle package is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be stored on the local disk. What pickle does is that it âserializesâ the object first before writing it to a file. Pickling is a way to transform a python o
2 min read