How Do You Write to a File with Python?

Problem scenario
You want to store variable values in a log file.  How do you get Python to write to a log file?

Solution
Use these lines in your Python program:

var1 = ‘contint’

with open(‘contint.log’, ‘a’) as contint_log:
    contint_log.write(“\n”)  # creates a new line
    contint_log.write(var1)
    contint_log.write(“Done writing variable.\n”) # template text

How Do You Capture Error Messages in a File of Your Choice Using Python Rather Than Have Them Printed to the Screen?

Problem scenario
You have a Python program that displays urllib3 (or some other type) of message to the screen.  You do not want them echoed there cluttering the screen.  You want to redirect the output to a log file (to permanently store the data).  How do you get a Python program to write to a log file?

Solution
Use these three lines, but replace “contint” with the name of your choice:

import logging

logging.basicConfig(filename=’contint.log’,

Why Cannot You Use a Python Module in a Program or the Command Prompt When One Will Work but Not the Other?

Problem scenario
You have a Python program use an import statement, but the program fails.  From the Python command prompt, you can import the module.  Why is it different?

Possible Solution #1
You may be using Python version 3 for either the command prompt or the program and Python version 2 for the complement (either the command prompt or the program). Try these commands:

python –version
python3 –version

Possible Solution #2
See where the potential imports are.

Why Do Linux Commands Not Work when They Work as Part of a Python Program with the subprocess.check_output Invocation?

Problem scenario
You are trying to debug a Python program that uses shell (or Bash) commands.  The shell or Bash commands work when they are called with Python’s subprocess.check_output.  They do not work when called directly from the Linux command prompt.  What is wrong?

Possible solutions
Normally I.T. professionals can get the Linux (Bash or shell) commands to work without as many moving parts.  In some instances the Python script that executes the Bash commands seem to help the reliability of the commands. 

How Do You Deal with the Python Error “IndexError: pop index out of range”?

Problem scenario
You have a Python list with n items.  You are getting a “IndexError: pop index out of range” error.  The order that these lists are being printed (used or displayed) is not what you expect.  Your program prints the entire list initially, so you know what to expect.  This IndexError and the order of the items being displayed is not what you expect.  Here is an example:

print(fun_list)
[‘item1’,

How Do You Troubleshoot the Python Error “NameError: name is not defined” When Using Boto3?

Problem scenario
You are running a Python program with Boto and you get an error like this:

  ” aws_access_key_id=AKIAJfoobar9999, NameError: name ‘AKIAfoobar9999’ is not defined”

What should you do?

Solution
Put single quotes around the aws_access_key_id value.

How Do You Demonstrate Recursion in a Sorting Algorithm Written in Python?

Problem scenario
You want to use recursion in Python in a sorting algorithm. How do you do this?

Solution
Save this program as contintsort.py and run it python contintsort.py. The code closer to the top is in Python 2, and the code closer to the bottom is in Python 3.

# Written by www.continualintegration.com
# Usage is self-explanatory. Just run the program. python contintsort.py
# No extra files are needed. …

How Does Python’s Range Function Work When Three Parameters Are Passed?

Problem scenario
You see this line of Python code:  for x in range ( len(coolList), 0, -1 ): print x
You are curious how that works and what that syntax means.

Answer
The first of the three parameters is the integer that corresponds to the starting number of the iteration.  The second variable is the range position that will terminate the iteration of the range function. 

How Do You Upgrade Python 2.6 to Python 2.7 on CentOS 6?

Problem scenario
On your Linux CentOS 6.x server you run “python –version” and find that Python 2.6 is installed.  You want Python 2.7 to be installed instead.  You do not want to use binaries (e.g., .rpm packages) or yum repositories. What do you do?

Solution
Run these commands:
curl -k https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz > Python-2.7.14.tar.xz
sudo mv Python-2.7.14.tar.xz /usr/bin/
cd /usr/bin/
sudo tar xf Python-2.7.14.tar.xz
cd Python-2.7.14
sudo ./configure –prefix=/usr/local
sudo make &&

How Do You Write Your Own Custom, Importable Module in Python?

Problem scenario
You want to build your own Python module.  You want this module to be able to be imported on your Linux server with Python (e.g., with “import contint”).  How do you write such a module?

Possible Solution #1
Write a .py file for the module, and place it where Python will look.  To find the location run these three commands:

python

import sys

print(sys.path)

# This command above will display directory paths for your .py file (the module that will be able to be imported).