In this tutorial, I will explain how to create a Python file in terminal. As a programmer, you often come across creating files and it is important to learn how to create a file in terminal. After researching I found several ways to achieve this task. I will explain the whole process step-by-step with examples that will help you to understand better.
Create a Python File in Terminal
Let us learn how to create a Python file in terminal.
Read How to Write a Variable to a File in Python?
Step 1: Open Your Terminal
On a Mac, you can open the Terminal application from the Applications folder or by searching for it using Spotlight. On Windows, you can use Command Prompt or PowerShell. If you’re using Linux, you can find the terminal in your applications menu.
Step 2: Navigate to Your Desired Directory
Before creating a Python file, navigate to the directory where you want to save it. You can use the cd (change directory) command. For example, if you want to create a file in a folder named PythonProjects located in your home directory, you would run:
cd ~/PythonProjectsIf the directory doesn’t exist, you can create it using:
mkdir ~/PythonProjects
cd ~/PythonProjectsCheck out How to Get File Size in Python?
Step 3: Create the Python File
To create a new Python file, you can use the echo command followed by the filename. For example, to create a file named data_analysis.py, you would run:
echo data_analysis.pyAlternatively, you can create and open the file in a text editor directly from the terminal. For example, using nano, you can create and edit the file simultaneously:
nano data_analysis.pyRead Python Get File Extension
Step 4: Write Your Python Code
Once you have your file open in the editor, you can start writing your Python code. For instance, let’s say you’re building a data analysis tool for a company named “Tech Innovations USA.” You might start with the following code to import libraries and define a function:
import pandas as pd
def load_data(file_path):
data = pd.read_csv(file_path)
return dataAfter writing your code, save your changes and exit the editor. In nano, you can do this by pressing CTRL + X, then Y to confirm saving, and Enter to exit.
Read Python Get Filename From Path
Step 5: Run Your Python File
To run your newly created Python file, use the following command:
python data_analysis.pyor, if you’re using Python 3:
python3 data_analysis.pyExample: Create a Simple Data Analysis Script
Let’s walk through a more complete example. Suppose you want to analyze sales data for “Tech Innovations USA.” You might have a CSV file named sales_data.csv with the following structure:
Date,Product,Sales
2024-01-01,Widget A,150
2024-01-02,Widget B,200
2024-01-03,Widget A,250Check out Python File methods
Step 1: Create the CSV File
First, create the CSV file using the terminal:
echo sales_data.csv
nano sales_data.csvCopy and paste the sales data into the file, then save and exit.
Step 2: Write the Analysis Code
Next, open your data_analysis.py file again:
code data_analysis.pyAdd the following code to read the CSV and calculate total sales:
import pandas as pd
def load_data(file_path):
data = pd.read_csv(file_path)
return data
def calculate_total_sales(data):
return data['Sales'].sum()
if __name__ == "__main__":
sales_data = load_data('sales_data.csv')
total_sales = calculate_total_sales(sales_data)
print(f"Total Sales for Tech Innovations USA: ${total_sales}")Check out Python file Does Not Exist Exception
Step 3: Run Your Script
Save the changes and run your script:
python data_analysis.pyYou should see the output:
Total Sales for Tech Innovations USA: $600
Additional Tips for Working with Python in the Terminal
Read How to Import a Class from a File in Python
1. Use Virtual Environments
For larger projects, it’s a good practice to use virtual environments to manage dependencies. You can create a virtual environment with:
python -m venv myenvActivate it with:
- macOS/Linux:
source myenv/bin/activate - Windows:
myenv\Scripts\activate
2. Edit Files with Different Editors
While nano is a simple editor, you might prefer using vim or emacs for more advanced features. For example, to create a file with vim, you would run:
vim data_analysis.pyOnce in vim, press i to enter insert mode, write your code, and then press ESC, type :wq, and hit Enter to save and exit.
Check out How to Copy File and Rename in Python
3. Use Code Editors with Terminal Integration
If you prefer a graphical user interface, consider using editors like Visual Studio Code or PyCharm, which offer integrated terminal support. This allows you to create and manage files within the editor while still utilizing terminal commands.
Conclusion
In this tutorial, I explained how to create a Python file in terminal. I discussed the step-by-step process of creating a Python file in the Terminal along with an example, I also gave additional tips for working with Python in the terminal.
You may read:
- How to Check If a File Exists and Create It If Not in Python?
- How to Get the File Size in MB using Python?
- How to Unzip a File in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.