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. …

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 system’s resources perspective.

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) …

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”)

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

Problem scenario
(The old problem scenario was “You want to write a program to call a unbound function in a new thread with the threading module. How do you do this?”)

You want to write a program to call a function in a new thread with the threading module. How do you do this?

Solution
Run this program (e.g.,

How Do You Get a PHP Program to Invoke a Python Program via Browsing a Website?

Problem scenario
You want Debian/Ubuntu Linux to support a website. You want a Python program to run every time a web page is downloaded. How do you get a PHP program to invoke a Python program on Debian/Ubuntu Linux?

Solution

Prerequisites
i. This assumes you have Apache2 and PHP installed. If you need assistance run this: sudo apt -y install apache2 php
ii.

How Do You Troubleshoot an Error Such as “UnboundLocalError: local variable ‘foobar’ referenced before assignment”?

Problem scenario
You run a Python program. You receive this message: “UnboundLocalError: local variable ‘foobar’ referenced before assignment”

What should you do?

Solution
Initialize the variable (e.g., foobar = 0). If you do so inside a function wherein foobar is called, that may work. If you want it to be available globally throughout your program, you will initialize it outside of a function.

How Do You Write a Python Program to Create New Processes with the multiprocessing Library?

Problem scenario
You want to create new processes with the multiprocessing library in Python. What should you do?

Solution
Run this program:

“”” In a duplicate terminal window, run this command before, during and after you run the pyhon script: sudo ps -ef | grep python | wc -l

The integer that returns will go up by two showing a new processes have been created when the program is still running. …

How Do You Write a Python Program to Test If Two Words Are Anagrams?

Problem scenario
You want a program to test if two words are anagramous with each other. You want the test to be case insensitive. How do you write a program that will interactively prompt the user for two different words and test if they are anagrams?

Solution
Use this Python 3 program (and it will interactively prompt you to enter two words):

word1 = input(“Enter one word: “).lower()
word2 = input(“Enter a second word: “).lower()

wL1 = list(word1)
wL2 = list(word2)

wL1.sort()
wL2.sort()

if (wL1 == wL2):
print(“The two programs are anagrams!”)
else:
print(“The two words are NOT anagrams!”)

It will not work with Python 2.