Creating Python Scripts via Terminal: A Practical Guide
Creating Python scripts via terminal is a fast way to automate repetitive tasks and keep your work organised in code. Instead of clicking through menus, you can trigger scripts that pull data, process files, and generate reports with a single command.
This guide focuses on creating and running Python files from the terminal on macOS, Linux, and Windows. You will also see how those scripts fit into real workflows, such as SEO reporting, data cleaning, and scheduled automation jobs.
Why Creating Python Scripts via Terminal Matters
Working with Python scripts directly in the terminal encourages small, focused programs that are easy to run and maintain. You can store these scripts in version control, share them with teammates, and schedule them on servers or local machines.
Once you are comfortable creating Python files in the terminal, you can chain them together into repeatable workflows. A script that cleans a CSV today can become the first step in a larger pipeline tomorrow.
The table below shows a few simple script ideas and how they support everyday tasks.
Example terminal-based Python scripts and their uses
| Script Name | Typical Input | Output / Result | How You Run It |
|---|---|---|---|
sitemap_check.py
|
XML sitemap URL list | List of broken or redirected URLs | Manual command or scheduled task |
keyword_cleaner.py
|
Raw CSV with keywords | Filtered, deduplicated keyword file | Manual terminal command |
serp_snapshot.py
|
List of target queries | Top results with URLs and titles | Daily or weekly schedule |
report_exporter.py
|
Analytics or log data | Formatted CSV or spreadsheet update | Triggered before reporting deadlines |
Keeping each script small and single-purpose makes debugging easier and allows you to reuse pieces across projects without rewriting everything from scratch.
Core Steps for Creating Python Scripts via Terminal
Regardless of platform, the process for creating Python scripts via terminal follows the same pattern. You open a terminal, create a file, add code, and run the file with Python.
Below is an ordered list that outlines the typical flow from idea to working script. You can apply this pattern to simple “hello world” programs or to more advanced automation.
-
Open your terminal application and move into a working folder using
cd. -
Create a new file with a
.pyextension using a command or editor. - Open the file in a text editor and add your Python code.
- Save the file and return to the terminal prompt.
-
Run the script with
pythonorpython3followed by the file name. - Check the output, adjust the code if needed, and rerun the script.
Once this routine feels natural, you can extend it with extras such as virtual environments, dependency files, and scheduled jobs, but the core steps stay the same.
Creating a Python Script in Terminal on macOS and Linux
On macOS and Linux, the terminal gives you direct control over file creation and editing. You can use built-in editors like nano
or vim
, or create files with touch
and edit them later.
First, open your terminal and move to a folder where you want to store your scripts, such as ~/projects/automation
. Use the mkdir
command once to create the folder, then cd
into it for your daily work.
To create a new Python file and open it in nano
, run nano my_script.py
. Type a simple test program like print("Python script running")
, save the file, and exit the editor. Back in the terminal, run python3 my_script.py
or python my_script.py
depending on your system configuration.
Making Python scripts executable on Unix-like systems
On macOS and Linux, you can make a Python script behave like a command. Add a shebang line at the top of the file, such as #!/usr/bin/env python3
, then use chmod +x my_script.py
to mark it as executable.
After that change, you can run ./my_script.py
directly from the terminal, as long as you are in the same folder or the folder is on your PATH. This approach is handy for scripts you run many times a day.
Creating Python Scripts via Terminal on Windows
On Windows, you can create Python scripts via terminal using Command Prompt, PowerShell, or Windows Terminal. The main difference from macOS and Linux is the default editor and the way paths are written.
First, confirm that Python is installed by running python --version
in your chosen terminal. If this command shows a version number, you are ready to create files. If not, install Python and enable the path option during setup so the terminal can find it.
Next, create a working folder such as C:\scripts
and move into it with cd C:\scripts
. To create a new file, run notepad my_script.py
. When Notepad asks if you want to create the file, choose yes, add a simple line like print("Hello from Windows")
, save, and close the editor.
Running and editing scripts efficiently on Windows
After creating the file, run python my_script.py
from the same folder. If the output appears as expected, you know the basic setup works. You can then extend the script with arguments, file reading, and external libraries.
As your projects grow, you may prefer using a code editor such as Visual Studio Code while still running scripts via the terminal. The workflow stays the same: edit in the editor, save, and switch to the terminal window to execute the script and view the results.
Basic Terminal Commands for Python Script Management
Once you start creating many Python scripts via terminal, a few file commands become part of your daily toolkit. These commands help you stay organised and avoid losing work.
The short list below highlights useful commands you will use again and again on most systems.
-
lsordirto list files in the current folder. -
cdto change into a different directory. -
mkdirto create a new folder for a project. -
rmordelto remove old or test files. -
cporcopyto duplicate a working script as a starting point.
Using these commands regularly keeps your script folders clean and makes it easier to find the correct file when you run or edit code from the terminal.
Example: Building a Simple Data Processing Script
To see how the pieces fit together, imagine you want a script that reads a CSV file and prints the number of rows. You can create this script entirely through the terminal and use it as a base for more advanced data tasks.
First, create a folder, move into it, and place a sample CSV file there. Next, create a new script file called row_counter.py
using your preferred editor command. Add code that imports csv
, opens the file, counts the rows, and prints the result.
Run the script with python row_counter.py
and check the output. If the count matches what you expect, you have a working pattern for future scripts that clean, filter, or transform data in more complex ways.
Extending the example with arguments
Once the basic script works, you can improve it by reading the file name from command-line arguments. Import the sys
module, read sys.argv[1]
as the file name, and use that value when opening the CSV.
This change lets you reuse the same script for many files by running commands like python row_counter.py data1.csv
and python row_counter.py data2.csv
. Small improvements like this make terminal-based scripts much more flexible.
Structuring a Folder of Terminal-Based Python Scripts
As you create more Python scripts via terminal, a simple folder structure helps you stay organised. Group related scripts together so you can see at a glance which files belong to a project.
The table below shows an example structure for a small automation project that handles sitemaps, reports, and data cleaning. You can adapt this idea to your own work.
Example folder layout for Python scripts
| Folder | Example Files | Purpose |
|---|---|---|
project_root/
|
README.md
, requirements.txt
|
Project overview and dependencies |
scripts/
|
sitemap_check.py
, row_counter.py
|
Main terminal-run Python scripts |
data/
|
input.csv
, output.csv
|
Input and output files for scripts |
logs/
|
run_2024-01-01.log
|
Optional logging for scheduled runs |
A clear layout like this makes it easier to write instructions for teammates and to remember how to run older scripts when you return to a project after several weeks.
Common Uses for Terminal-Driven Python Scripts
Once you know how to create Python scripts via terminal, you can apply the skill to many tasks beyond simple examples. The same workflow supports automation in data analysis, content work, and system checks.
For instance, a script can read a sitemap file, fetch each URL, and store status codes in a CSV. Another script might join two CSV exports into a single clean table that you use in a spreadsheet or dashboard.
Over time, these small scripts form a library of tools you can run on demand or schedule to run at night. The terminal becomes a control panel where one-line commands trigger all your core processes.
Scheduling and Sharing Terminal-Based Scripts
After you have a few useful scripts, the next step is to run them automatically. On macOS and Linux, you can use cron jobs. On Windows, you can use Task Scheduler. Both options call your Python script at a set time using the same commands you already tested by hand.
To share scripts with others, place them in a version control system such as Git. Teammates can clone the project, install the dependencies listed in a requirements file, and run the same commands in their own terminals.
This shared setup makes your automation more reliable, since everyone runs the same code and can see exactly what each script does before trusting the results.
Bringing It All Together
Creating Python scripts via terminal is a simple habit that pays off over time. You learn one basic pattern—create, edit, run, repeat—and apply it to many small problems in your work.
Whether you process CSV files, inspect sitemaps, or prepare data for reports, the terminal gives you a fast, repeatable way to run Python code. With a clear folder structure and a few core commands, you can turn one-off tasks into dependable scripts that save time every week.


