Skip to main content

Python 2.7 Scripting For System Administrators


Intro Video

Photo of Keith Thompson

Keith Thompson

DevOps Training Architect II in Content









Course Details

In this course, you will develop the skills that you need to write effective and powerful scripts and tools using Python. We will go through the necessary features of the Python language to be able to leverage its additional benefits in writing scripts and creating command line tools (data types, loops, conditionals, functions, error handling, and more). Beyond the language itself, you will go through the full development process including project set up, planning, and automated testing to build two different command line tools.



Continue If You're Interested in Python 2


Lesson Description:

Check out the Python 3 version of this course.

About the Course Author


Lesson Description:

Learn a little about me and what you can do to get the most out of this course.

Course Tools


Lesson Description:

Learn about the Linux Academy features that this course uses and general features that will improve your learning experience.



Lesson Description:

Get a brief overview of the course's content and flow.

Setting Up a Cloud Server Development Environment


Lesson Description:

Links for this VideoContent Repository .bashrc file .vimrc fileBefore diving into the course it's important that you have the following installed:Python 2.7 (pre-installed on modern Linux distributions) git wget which words (need file at /usr/share/dict/words) lsof text editor of your choiceThis video will show you how to utilize a cloud server as a development environment to follow along with this course.

Introducing Python

History and Benefits of Python


Lesson Description:

Before learning how to write scripts using Python, we cover the background of Python, the language's core feature set, and its non-programming benefits.

What's the Deal with Python 3?


Lesson Description:

We briefly talk about the issues that Python 3 set out to solve and the current state of Python.Links from this video:Python 2 or Python 3 What's new with Python 3

Just Enough Python

Introducing the REPL for Rapid Experimentation


Lesson Description:

Learn to utilize the Read Evaluate Print Loop (REPL) that Python provides to experiment with Python code and explore the language.

Creating and Running Python Scripts


Lesson Description:

Learn how to create Python scripts and run them using the Python CLI or as an executable.

Using Comments in Python


Lesson Description:

Learn how to document your Python code using comments.

Data Types - Strings and Numbers


Lesson Description:

Start learning about the built-in string and number types in Python.

Data Types - Booleans and None


Lesson Description:

Learn how to represent truth and nothingness in Python using the built-in Boolean and None types.

Working with Variables


Lesson Description:

Learn how to create variables in a Python script.

Lists and Tuples


Lesson Description:

Learn how to use the most common collections in Python: lists and tuples.

Dictionaries (dicts)


Lesson Description:

Learn to use Python dictionaries to organize key-value information.

Control Flow - Conditionals and Comparisons


Lesson Description:

Learn how to make comparisons in your Python code and branch your programs using Python's "if", "elif", and "else".

Control Flow - Loops


Lesson Description:

Learn how to run code repeatedly and iterate over collections by using loops.

Control Flow - Logic Operations


Lesson Description:

Learn how to perform logic operations in Python.

Exercise: Creating and Displaying Variables


Exercise: Working with If/Else


Exercise: Iterating Over Lists


Python 2.7 Scripting For System Administrators Python Basics


Scripting with Python

Reading User Input


Lesson Description:

Learn how to prompt the user of your scripts to enter information and how to use that information in your code.

Encapsulating Behavior Using Functions


Lesson Description:

Improve the organization of your code and make it easy to reuse logic by creating functions in Python.

Using Standard Library Packages


Lesson Description:

Learn to leverage a massive amount of built-in functionality by pulling in extra software packages from Python's standard library.

Working with Environment Variables


Lesson Description:

Learn how to interact with your user's environment by reading in environment variables.

Interacting with Files


Lesson Description:

Learn to read, write, and append to files using Python.Note: if you're using Python 3, the following line will not work:, os.SEEK_END)
The equivalent for Python 3 would be:, os.SEEK_END)

Parsing Command Line Parameters


Lesson Description:

Improve the user experience of your scripts by accepting command line parameters and displaying help text.

Handling Errors with try/except/else/finally


Lesson Description:

Create better scripts by anticipating errors that will occur and making your scripts handle them gracefully.

Execute Shell Commands from Within Python


Lesson Description:

Leverage code that isn't written in Python for your scripts by learning how to invoke other shell commands.

Exit Statuses


Lesson Description:

Make your scripts more useful by returning useful error codes when something goes wrong by setting exit statuses.

Advanced Iteration with List Comprehensions


Lesson Description:

Note: your /usr/share/dict/words file might not be exactly like mine so don't be surprised if you get slightly different results. Learn to use "list comprehensions" in Python as a way to more succinctly iterate over your lists to return new values.Installing /usr/share/dict/wordsThe words file might not be installed on your system (such as a CentOS 7 cloud server), but you can install it using:

$ sudo yum install -y words
On a debian system, you'll to run this command to populate the /usr/share/dict/words file:
$ sudo apt-get install --reinstall wamerican-insane

Standard Library: random & json


Lesson Description:

Learn to utilize more of the standard library by creating JSON files with random contents as part of a short example project.

Standard Library: shutil & glob


Lesson Description:

Learn to search files by name using (glob) patterns and find replacements in the standard library for common UNIX tools like mv, mkdir, cp, and rm.

Standard Library: re & math


Lesson Description:

Learn some of the more common math functions and how to potentially use regular expressions in your Python.

Exercise: Creating and Using Functions


Exercise: Using the 'os' Package and Environment Variables


Exercise: Creating Files Based on User Input


Exercise: Handling Errors When Files Don't Exist


Exercise: Interacting with External Commands


Exercise: Setting Exit Status on Error


Python 2.7 Scripting For System Administrators Python Scripting and IO


Third Party Packages

Installing 'pip' to Manage Python Packages


Lesson Description:

Learn how to install the `pip` package manager so that you can start working with third-party packages.

Installing Third-Party Packages Using 'pip'


Lesson Description:

Learn to use `pip` to install third-party packages, leveraging the open-source community.



Lesson Description:

Working with the system Python works fine, unless you need to have multiple versions of a Python package. Virtualenvs are Pythons way of sandboxing dependencies.

Using Third-Party Code in Your Scripts


Lesson Description:

Learn to utilize the third-party packages in your own scripts by using the popular `requests` library.

Exercise: Installing Third-Party Packages


Exercise: Utilizing Third-Party Packages


Python 2.7 Scripting For System Administrators Python Packages and Dependencies


Creating a Larger Scripting Project

Examining the Problem and Planning


Lesson Description:

For this final section, we'll be building a full project from planning to distribution. The first thing that we need to do is plan out our approach and set up some of our non-Python dependencies.

Structuring the Project


Lesson Description:

Time to set up our Python project. We'll be creating a virtualenv, documenting our project, creating our repository, and making our project installable via pip.

Test Driving the Command Line Parser (Part 1)


Lesson Description:

The first unit of our application that we'll set up is our CLI's parser. We will be implementing this (and the rest of the units) using Test Driven Development (TDD) techniques. In this video, we'll set up our testing environment and implement the initial version of our parser.

Test Driving the Command Line Parser (Part 2)


Lesson Description:

We continue working on our command line parser by doing a little refactoring.

Implementing PostgreSQL Interaction


Lesson Description:

Working on our second unit, we'll be implementing our tool's database interaction by interfacing with the `pg_dump` tool.

Implementing Local File Storage


Lesson Description:

Use TDD to implement the way we store files locally.

Implementing S3 Storage


Lesson Description:

Use TDD to implement the way we store files on AWS S3.

Wiring the Units Together


Lesson Description:

With all of the individual units built and tested, it's time to combine them to build our final CLI implementation.

Building and Sharing a Wheel Distribution


Lesson Description:

Learn how to build a distribution of the pgbackup project so that it can easily be shared and installed using pip.

Installing Python 3 on CentOS 7


Lesson Description:

Learn to build Python 3 from source on a CentOS system. This prepares you for adding Python 3 support to the pgbackup package.

(Optional) Installing Python 3 on Debian


Lesson Description:

Learn to build Python 3 from source on a Debian system. This prepares you for adding Python 3 support to the pgbackup package. Mark this video as complete if you've already installed Python 3.

Testing Against Python 2 and Python 3


Lesson Description:

We've been working with mostly Python 2, but that doesn't mean that the same code can't be run with Python 3. Let's take a look at how we can test our code under both Python versions using the `tox` package.

Exercise: Creating a Python Project


Exercise: Test Drive CLI Parser


Exercise: Implementing User Management


Exercise: JSON Parsing and Exporting


Exercise: Creating the Console Script


Exercise: Building a Wheel Distribution


Scripting For System Administrators Python Projects Testing and Distribution



Thank You and Next Steps


Lesson Description:

Congratulations on finishing this course. Don't forget to review the content and tell the community what you learned and thought of this course. Have fun writing Python!

Take this course and learn a new skill today.

Transform your learning with our all access plan.

Start 7-Day Free Trial