Tag: Python

Python clean up old files

leaf

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())        
        f.remove()        
        removed += 1     
  print ("Removed " + str(removed) + " files.")
if __name__ == "__main__":  
  main()
Advertisements

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')
  print(responseData)

if __name__ == "__main__":
 main()