Tag Archives: Python

Python 3 File Read Write with Urllib

This post is inspired by my previous post on utilizing urllib2 to download a sequence of files programatically. As you probably know, the transition from Python2 to Python3 has left many people struggling to port their code, so I thought I would re-hash some of my old posts and provide Python3 versions of my code examples. One resource I found recently that really helped me is the online version of Mark Pilgrim’s “Dive into Python3”, specifically the chapter on porting your 2.x code to Python3.

The example provided below outlines how to use the urllib library included within Python3 to download a sequence of image files along with comments to describe what is going on.

#import urllib request 
import urllib.request
#import urllib error handling
from urllib.error import HTTPError,URLError

#function that downloads a file
def downloadFile(file_name,file_mode,base_url):
    #create the url
    url = base_url + file_name
    
    # Open the url
    try:
            f = urllib.request.urlopen(url)
            print("downloading ", url)
    
            # Open our local file for writing
            local_file = open(file_name, "w" + file_mode)
            #Write to our local file
            local_file.write(f.read())
            local_file.close()
    
    #handle errors
    except HTTPError as e:
            print("HTTP Error:",e.code , url)
    except URLError as e:
            print("URL Error:",e.reason , url)

# Set the range of images to 1-50.It says 51 because the
# range function never gets to the endpoint.
image_range = list(range(1,51))

# Iterate over image range
for index in image_range:
    base_url = 'http ://www.techniqal.com/'
    #create file name based on known pattern
    file_name =  str(index) + ".jpg"
    # Now download the image. If these were text files,
    # or other ascii types, just pass an empty string
    # for the second param ala stealStuff(file_name,'',base_url)
    downloadFile(file_name,"b",base_url)

The key things to learn about converting my old example to the new are outlined below. This was a learning exercise for me, and will hopefully provide enough context for you to understand how to port your own code to Python3.

  • There are obvious changes on how to use Urllib vs the old Urllib2 methods. Take a peek at “Dive into Python3” for more details. He does a much better job describing it than I ever could.
  • Print statements are now called as a function.

    Python2:

    print "My Variable is equal to " + myVariable
    

    Python3:

    print("My Variable is equal to ", myVariable)
    
  • Except blocks are handled differently when using a try/except.

    Python2:

    except HTTPError, e:
    		print "HTTP Error:",e.code , url
    

    Python3:

    except HTTPError as e:
    		print("HTTP Error:",e.code , url)
    
  • The range() function used to return a list , but now returns an iterator object. If you still want to get a list from the range function, see below.

    Python2:

    myRangeList = range(1,100)
    

    Python3:

    myRangeList = list(range(1,100))
    

I’m not a software engineer by trade, so please excuse any syntax oddities. I appreciate any feedback, or more graceful ways to write this code. Leave them in the comments and I’ll happily update my example.

You Just Got Tagged

Tara recently wrote about how she was tagged by someone else in a blog post, which is apparently the equivalent of poking someone with a stick via the internet. Normally I don’t fall in for the “chain letter / poking / link love/ tagging” behavior. This time though, Tara used the correct method to goad a response out of me… She dared me. And I am no chicken. So below, you will see my 4×4 response. Sit back, relax, and enjoy learning a little bit more about me. Don’t worry, I won’t tag anyone else, as I don’t want to condone Tara’s behavior, but Leslie, Derek, and Walter should step up.

4 Things I Love:

  • My 3 month old son Lucas: He wakes up with a smile every day, and reminds me to do the same.
  • My wife: She keeps me grounded and constantly reminds me of what is most important in life.
  • My dogs: They were our first “kids” and we will always love them, even though they don’t get quite as much attention as they used to.
  • My Job: As cheesy as it sounds, I enjoy my job, and the people I work with. We have some of the most talented and funny people I have ever met working here at Lijit. Every day is fun(nearly).

4 Video Games I Love:

  • Katamari (all of them): The funky music, and addictive game play make up one of the most original series of games ever made. Replay value is HIGH.
  • Grand Theft Auto (all of them): The sandbox style changed the way everyone views video games, and continues to push other publishers to innovate.
  • Halo (all of them): Single player – meh, kinda fun. Multiplayer – f*cking awesome.
  • Bejeweled: Oh crack of the internet, how many productive work hours have you stolen from me. From the little web game that stole my heart and wasted many, many hours of my time at work, to the mobile version on my old Treo 650 that I used to play when I would go outside for cigarette breaks. Don’t worry, I got rid of the cigarette habit and the Treo.
  • Honorable mention: Rockband is awesome.

4 Programming Languages I Couldn’t Live Without:

  • Python: I learned more and got more done with this flexible language than with any other language.
  • PHP: If it weren’t for php, I wouldn’t be able to show this post to you. This is also the language that re-opened my eyes, and got me into web programming, and consequently pushed me towards python, and saved me from perl. Watch out for perl zealots attacking. Just by bashing perl ,I am pretty sure I can triple the readership of this post. Ooh, and Java, it sucks too.
  • Javascript: Yeah, I know, not a “real” programming language in the classic sense. But the things it allows you do in modern browsers is nothing short of spectacular. A lot of us wouldn’t have jobs if it weren’t for javascript.
  • Basic: If not for this language, and the fact that my dad taught some of it to me on our Timex Sinclair, I may not be working with computers at all.


4 Online Tools I Like:

Twitter: You satisfy both my voyeuristic and egotistical urges. Does anyone really care what I am doing…
gMail: All other online mail truly sucks in comparison. Plus your pop3 and your imap is sweet.
del.icio.us: The most practical of all online tools. It started out before Web 2.0 was cool and will be around long after it’s gone.
Lijit: I cannot in good conscience omit the source of my livelihood and the reason my baby has shoes. Plus our search is pretty damn good too.

That is all. I don’t know how to finish this blog post. So I will leave you with one my favorite quotes that will only confuse you more as I am a vegetarian.
Mark TwainSacred cows make the best hamburger .

Ruby and Rails Float Python’s Boat

python_winner.gif
Antonio over at antoniocangiano.com has a great article about the effect the rise of Rails and Ruby has had on Python.

I totally agree with him regarding the meteoric rise in popularity of Rails buoying scripting languages such as python. I believe more and more businesses are choosing, or are open enough, to implement Rails and Django solutions for their webapps. On top of other MVC frameworks, both Django and Rails allow for fast and sometimes cheap development (the non-cheap comes in if you have to hire an over-priced rails consultant). The visibility is definitely changing the way businesses view scripting languages and allow them to be seen as a viable alternative to Java and other enterprise adopted technologies.

I have had a man-crush on python for quit some time, and try to spout off about it’s benefits whenever I can.
I have been so close to starting a series of blog posts surrounding Python and the ease of adoption for the non-programmer. As soon as I have more time…

Great Python Articles/Tutorials

I thought I would throw some information out there that has helped me immensely in the past. Here is a link to some articles on Devshed.com that simplify and de-mystify some common problems people have when learning Python, from beginner on up. For beginners I suggest checking out the articles here and here for info on file handling and sockets.
Also, check out the forums if you are ever in need of some guidance or help in the world of Python , or just about any other language/technology.

–List of articles–

Python: Simple File Read and Write

python
Though there are a lot of resources out there for Python,and great books, rarely do you find a simple reference guide that helps you navigate the basics. I found that there was not a lot of reference material to help you do the simple things, if you didn’t know what to look for.

This little guide will focus on the basics of reading and writing files in python..
Continue reading Python: Simple File Read and Write