[Tutor] Confused about error message

gelsey torres modern.teenage.cyberpunk at gmail.com
Mon Jul 4 03:37:57 CEST 2005


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. Here is my code:

#! /usr/bin/python
# 07/03/2005
# file_downloader.py - Downloads files and unzips them if they are a .zip file

from urllib import FancyURLopener
import os.path
import zlib
import fnmatch

def get_file(address, filename, url):
    try:
        file_content = FancyURLopener()
        file_content.retrieve(address, filename)
        print "Finished downloading %s from %s." % (filename, url)
    except(), error:
        print "An error has occured:", error
        
def unzip_file(filename):
    print "Unzipping file.."
    for file in filename:
        try:
            file = os.path.split(filename)[2]
            file += zlib.decompressobj()

def main():            
    address = raw_input("Enter the address of the site: ")
    url, filename = os.path.split(address) # remove path and keep the
name of the original file
    get_file(address, filename, url)
    if fnmatch(filename, "*.zip"):
        unzip_file(filename)
    else:
        pass
    
main()
response = raw_input("Do you want to run the program again? (y/n): ")
while (response == "y"):
    main()     
else:
    raw_input("\n\nPress enter to quit.")

When I ran it to test main(), I got this error:

File "file_downloader.py", line 25
    def main():            
    ^
SyntaxError: invalid syntax

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. What am I doing wrong? I'm using Python 2.3 on Mac OS X version
10.3.9, and I run my scripts from Terminal.

Thank you for your consideration,

Gelsey


More information about the Tutor mailing list