Tag: Python

Python try catch loop with increasing wait time


The idea here is if there is an occasional error that causes your whole procedure to abort, but it is just an intermittent glitch that will work just fine if you wait a bit, you can just trap for the error and retry a few times.

But each time you retry, the wait time increases, since you don’t exactly know when the issue will clear.  This is with Python 2.7.10.

import time

tries = 1
wait = [1, 5, 10, 15]
while tries <= 5:
        raise ValueError('Oops!')
        if tries < 5:
            print "Retry after " + str(wait[tries - 1]) + " seconds."
            time.sleep(wait[tries - 1])
        tries = tries + 1


Here’s the output:

Retry after 1 seconds.
Retry after 5 seconds.
Retry after 10 seconds.
Retry after 15 seconds.

In real life, the differences between the wait times are bigger.  I did 1 minute, 5 minutes, 30 minutes, and an hour.

Also of course you would have your real code inside the try/except.  The raise ValueError(‘Oops!’) is just to test the concept of the try/catch loop with increasing wait times.

If you know the exact exception, you can trap for it specifically, like below:

except ValueError as e:

where ValueError is substituted with the actual exception you are trapping for.


Python clean up old files


Was learning Python and wrote this script to clean up files.  It’s not awesome.  Has no error handling for when the target file is already open.  Just kinda barfs when that happens.  So it needs work.  This was written in Python 3.

#This script uses a library called path.py.  Get it from here: https://pypi.python.org/pypi/path.py
#Download the Python Wheel file.  At this writing, it is called path.py-8.2.1-py2.py3-none-any.whl.  
#Install using the command: pip install <name of wheel file>.
#You must have Python installed first.  Been a while since I installed it, but here is the official place to 
#get it from: https://www.python.org/downloads/
#Once you have everything installed and the script works, use Windows Task Scheduler to run it, so you don't forget.

import timefrom 
path import path

def main():  
  # Because in python, the \ is an escape character, we need to use two of them in directory specifications  
  paths = ["c:\\jboss\\jboss-eap-5.1\\jboss-as\\server\\default\\tmp","c:\\jboss\\jboss-eap-5.1\\jboss-as\\server\\default\\log"]  
  DAYS = 7  
  time_in_seconds = time.time() - (DAYS * 24 * 60 * 60)  
  removed = 0  
  for p in paths:    
    print (p)    
    d = path(p)      

    for f in d.walk():      
      if f.mtime <= time_in_seconds:        
        print ("Deleting " + f.basename())        
        removed += 1     
  print ("Removed " + str(removed) + " files.")
if __name__ == "__main__":  

Learning Python – make a URL request

rosetwoandbuds002  Been learning Python lately.  Have done a couple of tutorials and one or two simple programs.  This post will be about a simple example of how to access some real estate information that is available from a company called Trulia.

First, some links to sites that helped me figure this out:

  • Download Python, install, and some reference documentation: https://www.python.org/downloads/
  • Stackoverflow thread about making a URL request in Python: http://stackoverflow.com/questions/17301938/making-a-request-to-a-restful-api-using-python
  • How to make a Trulia RSS feed with your real estate search criteria: https://www.trulia.com/tools/rss/#code_block
  • Some free python IDEs: http://insights.dice.com/2014/09/23/look-5-free-python-editors/
  • I decided to go with Visual Studio for an IDE. I picked the custom installation so I could specify Python as a supported language.  https://www.visualstudio.com/downloads/

The code:

#Just a little POC to retrieve some real estate information from the web
#Python 3
import urllib.request #for opening connections to URLs

def main():

  truliaURL = 'https://www.trulia.com/rss2/for_sale/Burbank,CA/'

  request = urllib.request.Request(truliaURL)
  response = urllib.request.urlopen(request)
  responseData = response.read().decode('utf-8')

if __name__ == "__main__":