How Do You Troubleshoot the Python Error “subprocess.CalledProcessError: Command ‘…’ returned non-zero exit status 1”?

Problem scenario
You are automating a Linux infrastructure task with Python using subprocess calls. You get this error “subprocess.CalledProcessError: Command ‘…’ returned non-zero exit status 1”, what should you do?

Solution
Run the Linux command without Python. Then run echo $? to determine the exit code. If you see a 1, that means Python notices this command is not considered to have run successfully.

 » Read more..

How Do You Retrieve an Application-Level Secret from AWS?

Problem scenario
You want to obtain a non-database secret from AWS and you know the name of the secret (in Secrets Manager) and the region it is in. What should you do?

Solution
Prerequisites
You have installed and configured the AWS CLI. If you need assistance with this, click on this posting if you can use pip or this posting if you cannot use pip.

 » Read more..

Python Tips when Dealing with Lists

Here are some tips when using Python and dealing with lists.

  1. Never call a variable “list”. (If you are using a variable for a list, use something like lista or list1 — not “list”.) Any word that is not a reserved word and not a built-in data type will work. You need to know the reserved words in Python because if you accidentally use them, it can cause a problem.

 » Read more..

In Python How Do You Print Just The Values of The Two-dimensional List without The Quotes, Commas or Brackets?

Problem scenario
In Python your program has a two-dimensional list. It prints out like this:

[‘ ‘, ‘X’, ‘ ‘, ‘ ‘, ‘ ‘]
[‘ ‘, ‘X’, ‘X’, ‘ ‘, ‘ ‘]
[‘ ‘, ‘X’, ‘X’, ‘X’, ‘ ‘]
[‘ ‘, ‘X’, ‘X’, ‘X’, ‘X’]

You want it to print out like this:

X
XX
XXX
XXXX

Solution
Assuming that there are n rows of your matrix (aka two-dimensional array),

 » Read more..

In Python How Do You Print Three Integers Separated by a Space on the Same Line?

Problem scenario
You want to print three integers. You do not want to convert them to be part of a string. You want a space to separate each integer. How do you do this in Python?

Solution
Assuming your variables are x, y, and z, and they are integers, use a line like this:

print(x, y, z)

Here is a complete program that proves it works:

x = 5
y = 3
z = 100
print(x,  » Read more..

How Do You Write a Python Program to Download the Images of a Website?

Problem scenario
You want to download pictures (e.g., .png, .jpeg etc.) from websites. How do you use Python to download such files?

Solution
Use this program with a subdirectory in the directory that this program resides to receive the picture files.

“””
dwldimages.py
Downloads all the images on the supplied URL, and saves them to the
specified output file (“/test/” by default)

Usage:
python dwldimages.py http://example.com/ /tmp/test/ # where /tmp/test/ is the directory you want to save the image files to.  » Read more..

How Do You Create a REST API Endpoint?

Problem scenario
You want a REST API endpoint. How do you create a URL that is a proof-of-concept to trigger a Python program?

Solution
Prerequisites
i. You have installed Python 3. If you are using CentOS/RHEL/Fedora, run this: sudo yum -y install python3
ii. You need a web server installed. If you do not have one,

 » Read more..

In Python, What Are Some Disadvantages to Using os.execlp to Fork a Process?

Question
In Python you are familiar with importing the os module and using different exec variations. What are some reasons that you would not use os.execlp?

Answer
1. If you use os.execlp to call another program, that program is more likely to return “Killed”. The resources of the child process are, by default, more limited in part because the fork operation is expensive from a systems resources perspective.

 » Read more..

How Do You Get Python to Compute How Long Something Took?

Problem scenario
You want to check the runtime duration of a section of Python code. How do you compute the amount of time something took in Python?

Solution
Write a program like this:

import datetime, time
t1 = datetime.datetime.now()
time.sleep(5) # replace this line with the section of code you want to time
t2 = datetime.datetime.now()
t3 = t2 – t1
print(“Time format is in hours:minutes:seconds:seconds_decimals”)
print(t3)  » Read more..

In Python, How Do You Call a Bound Function as a New Thread with the thread Module?

Problem scenario
You want to write a program to call a bound function in a new thread. How do you do this?

Solution
Run this program (e.g., python foobar.py):

import _thread as thread
class mighty:
def cool():
print(“Cool.”)
def contint():
print(“Hello!”)
if name == “main”:
foobar = thread.start_new_thread(mighty.cool, () )
print(“thread finished…exiting”)

 » Read more..