Automating Routine Tasks with Python Scripts
Learn how to automate routine tasks with Python scripts and file operations.
Introduction
In this technical article, we will explore how to automate routine tasks with Python scripts. Automating routine tasks can save you time, reduce errors, and increase productivity. Python is a great choice for automation, as it is easy to learn, versatile, and powerful.
We will learn how to create and run Python scripts, how to use Python for file and directory operations, how to handle command line arguments, and how to create scripts for common tasks. By the end of this article, you will be able to start automating your own routine tasks with Python scripts.
Understanding Python Scripts
A Python script is a file that contains Python code that can be executed by the Python interpreter. A Python script usually has a .py extension and can contain any valid Python code, such as variables, functions, classes, modules, etc.
Python scripts are different from interactive Python code, which is entered and executed in a Python shell or an interactive development environment (IDE). Interactive Python code is useful for testing and debugging, but it is not persistent and cannot be reused easily. Python scripts are persistent and can be reused and modified as needed.
Creating a Python Script
To create a Python script, you need a text editor or an IDE that supports Python syntax highlighting. You can use any editor or IDE of your choice, such as VSCode, Sublime Text, PyCharm, etc.
The first step to create a Python script is to write the shebang line at the top of the file. The shebang line tells the operating system which program to use to execute the file. For example, if you are using Linux or Mac OS, you can write:
#!/usr/bin/env python3This tells the operating system to use the python3 program in the current environment to run the file. If you are using Windows, you can skip the shebang line, as Windows uses the file extension to determine which program to use.
The next step is to make the script executable. This means giving the script permission to run as a program. To do this, you need to use the chmod command in the terminal. For example, if your script is named hello.py, you can type:
chmod +x hello.pyThis gives the script permission to execute for the user, group, and others.
The final step is to write some Python code in the script. For example, let’s write a simple script that prints a greeting:
# A simple script that prints a greeting
print("Hello, world!")Save the script with a .py extension and you are done!
Using Python for File and Directory Operations
In previous articles, we learned how to use Python for file and directory operations, such as creating, deleting, copying, moving, or renaming files and directories. We also learned how to use Python’s built-in functions and libraries, such as os, shutil, and glob, to manipulate files and directories.
We can incorporate these file and directory operations into our Python scripts to automate various tasks. For example, we can write a script that creates a backup of a directory by copying all its files and subdirectories to another location. Here’s an example of such a script:
# A script that creates a backup of a directory
import os
import shutil
# Define the source and destination directories
source_dir = '/path/to/source'
destination_dir = '/path/to/destination'
# Create the destination directory if it doesn't exist
if not os.path.exists(destination_dir):
os.mkdir(destination_dir)
# Iterate through all files and subdirectories in the source directory
for root, directories, files in os.walk(source_dir):
# Loop through each subdirectory
for directory in directories:
# Get the full path of the subdirectory
directory_path = os.path.join(root, directory)
# Create the corresponding subdirectory in the destination directory
shutil.copytree(directory_path, os.path.join(destination_dir, directory_path))
# Loop through each file
for file in files:
# Get the full path of the file
file_path = os.path.join(root, file)
# Copy the file to the destination directory
shutil.copy(file_path, destination_dir)
# Print a message when done
print(f"Backup completed from {source_dir} to {destination_dir}")This script uses the os.walk() function to iterate through all files and subdirectories in the source directory, the shutil.copytree() function to create subdirectories in the destination directory, and the shutil.copy() function to copy files to the destination directory.
Command Line Arguments in Python Scripts
Command line arguments are parameters that are passed to a script when it is executed from the command line. Command line arguments can be useful for customizing the behavior of a script or providing input data to a script.
Python provides a built-in module called argparse that simplifies the process of handling command line arguments. The argparse module allows us to define the arguments that our script accepts, parse them from the command line, and access them in our script.
To use the argparse module, we need to follow these steps:
Import the
argparsemodule usingimport argparse.Create an
ArgumentParserobject usingargparse.ArgumentParser(). This object holds all the information needed to parse the command line arguments.Add arguments to the
ArgumentParserobject using theadd_argument()method. This method allows us to specify the name, type, default value, help message, and other properties of each argument.Parse the command line arguments using the
parse_args()method. This method returns a namespace object that contains the values of the arguments as attributes.Access the values of the arguments using dot notation on the namespace object.
Here’s an example of how to write a script that accepts two command line arguments: a source directory and a destination directory:
# A script that accepts two command line arguments: a source directory and a destination directory
import argparse
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="A script that copies files from one directory to another")
# Add arguments to the parser
parser.add_argument("source", help="The source directory")
parser.add_argument("destination", help="The destination directory")
# Parse the command line arguments
args = parser.parse_args()
# Access the values of the arguments
source_dir = args.source
destination_dir = args.destination
# Print the values of the arguments
print(f"Source directory: {source_dir}")
print(f"Destination directory: {destination_dir}")To run this script from the command line, we need to provide the values of the arguments after the script name. For example:
./script.py /path/to/source /path/to/destinationThis will print:
Source directory: /path/to/source
Destination directory: /path/to/destinationWe can also use the -h or --help option to display a help message that shows the usage and description of each argument:
./script.py -hThis will print:
usage: script.py [-h] source destination
A script that copies files from one directory to another
positional arguments:
source The source directory
destination The destination directory
optional arguments:
-h, --help show this help message and exitCreating Scripts for Routine Tasks
Now that we know how to create and run Python scripts, how to use Python for file and directory operations, and how to handle command line arguments, we can create scripts for routine tasks that we perform frequently.
For example, let’s create a script that organizes a directory based on file type. This script will accept a source directory as a command line argument and create subdirectories for each file type in the source directory. Then, it will move all files of each type into their respective subdirectories.
Here’s an example of such a script:
# A script that organizes a directory based on file type
import argparse
import os
import shutil
# Create an ArgumentParser object
parser = argparse.ArgumentParser(description="A script that organizes a directory based on file type")
# Add an argument to the parser
parser.add_argument("source", help="The source directory")
# Parse the command line argument
args = parser.parse_args()
# Access the value of the argument
source_dir = args.source
# Get the list of files in the source directory
files = os.listdir(source_dir)
# Create a set to store the file types
file_types = set()
# Loop through each file
for file in files:
# Get the file extension
file_extension = os.path.splitext(file)[1]
# Add the file extension to the set of file types
file_types.add(file_extension)
# Loop through each file type
for file_type in file_types:
# Create a subdirectory for each file type
subdirectory_path = os.path.join(source_dir, file_type)
os.mkdir(subdirectory_path)
# Loop through each file again
for file in files:
# Get the file extension
file_extension = os.path.splitext(file)[1]
# If the file extension matches the file type, move the file to the subdirectory
if file_extension == file_type:
file_path = os.path.join(source_dir, file)
shutil.move(file_path, subdirectory_path)
# Print a message when done
print(f"Directory organized by file type: {source_dir}")This script uses the os.path.splitext() function to get the file extension of each file, the os.mkdir() function to create subdirectories for each file type, and the shutil.move() function to move files to their respective subdirectories.
Running Python Scripts from the Command Line
To run our Python scripts from the command line, we need to follow these steps:
Open a terminal window and navigate to the directory where our script is located.
Type
./script_name.pyfollowed by any command line arguments (if any) and press enter. For example, to run our script that organizes a directory based on file type, we can type:
./organize_by_file_type.py /path/to/sourceWait for the script to execute and display its output or any errors (if any).
Some potential issues and troubleshooting tips when running Python scripts from the command line are:
Make sure the script name and any command line arguments are spelled correctly and have valid values.
Make sure the Python interpreter and any modules or libraries used in the script are installed and up-to-date.
Use print statements or a debugger to check the values of variables and the flow of logic in the script.
Conclusion
In this article, we explored how to automate routine tasks with Python scripts. We learned how to create and run Python scripts, how to use Python for file and directory operations, how to handle command line arguments, and how to create scripts for common tasks. By mastering these skills, you can save time, reduce errors, and increase productivity.
I hope you enjoyed this article and found it useful for your Python projects. I encourage you to try creating your own Python scripts for routine tasks and share your experiences in the comments below. If you have any questions or topics you’d like to see covered in future posts, please let me know. Thank you for reading and happy coding!

