[Tutor] Confused about error message

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Jul 4 09:57:04 CEST 2005

On Sun, 3 Jul 2005, gelsey torres wrote:

> I'm new to Python and computer programming in general. Eventually I got
> sick of just reading my programming books and typing code from them into
> the interpreter, so I decided to write a script that will grab files
> from websites and save them to the hard drive, and if they're
> compressed, unzip them.

Hi Gelsey,

[looking at code]

Wow, that's pretty impressive as a beginning program!

As Kent mentioned, the exception handling code that you've written is
slightly misindented with some other syntax errors.  But I'd strongly
recommend just stripping it out altogether for the moment until you have a
need for exception handling.

I'm taking a look at the get_file() function now:

def get_file(address, filename, url):
    file_content = FancyURLopener()
    file_content.retrieve(address, filename)
    print "Finished downloading %s from %s." % (filename, url)

Ok, makes sense so far.

unzip_file() looks like it'll need some work, so I'll skip that one for
now.  Let's concentrate on getting this working for regular files first.

The main function has a slightly unusual line here:

    url, filename = os.path.split(address)

I see what you're trying to do: you're trying to take something like:


and break it down into:

    ["http://python.org/", "index.html"]

The only issue is that os.path.split() works with file paths, not URLs.
Some operating systems don't use forward slash as the path separator, so
the code is a little fragile.

A more reliable way to do this splitting is with the 'urlparse' library:


But in a pinch, you can also just split the string along slashes, since
the URL standard says that slashes are special.  Here's a quick-and-dirty
function to do that:

>>> def mysplit(address):
...     """Breaks a full URL address into the url and filename
...     portions."""
...     pieces = address.split("/")
...     url = '/'.join(pieces[:-1])
...     filename = pieces[-1]
...     return (url, filename)
>>> mysplit("http://python.org/index.html")
('http://python.org', 'index.html')

> Although sometimes it takes me awhile, I can usually spot the errors
> that I've made when they come up. I've stared at this one for two days
> and I still don't see the syntax error the interpreter is referring to.

Feel free to show us code on Tutor;  there's no reason you have to suffer
through SyntaxErrors like that.  Don't wait for two days.  *grin*

Best of wishes to you!

More information about the Tutor mailing list