[Tutor] Confused about error message
Ewald Ertl
ewald.ertl at hartter.com
Mon Jul 4 09:44:13 CEST 2005
Hi!
you're missing the except-clause in the exception-handling:
on Sun, 3 Jul 2005 21:37:57 -0400 gelsey torres <modern.teenage.cyberpunk at gmail.com> wrote :
---------------------------------------------------------------------------------------------
gelsey torres > I'm new to Python and computer programming in general. Eventually I
gelsey torres > got sick of just reading my programming books and typing code from
gelsey torres > them into the interpreter, so I decided to write a script that will
gelsey torres > grab files from websites and save them to the hard drive, and if
gelsey torres > they're compressed, unzip them. Here is my code:
gelsey torres >
gelsey torres > #! /usr/bin/python
gelsey torres > # 07/03/2005
gelsey torres > # file_downloader.py - Downloads files and unzips them if they are a .zip file
gelsey torres >
gelsey torres > from urllib import FancyURLopener
gelsey torres > import os.path
gelsey torres > import zlib
gelsey torres > import fnmatch
gelsey torres >
gelsey torres > def get_file(address, filename, url):
gelsey torres > try:
gelsey torres > file_content = FancyURLopener()
gelsey torres > file_content.retrieve(address, filename)
gelsey torres > print "Finished downloading %s from %s." % (filename, url)
gelsey torres > except(), error:
gelsey torres > print "An error has occured:", error
gelsey torres >
gelsey torres > def unzip_file(filename):
gelsey torres > print "Unzipping file.."
gelsey torres > for file in filename:
gelsey torres > try:
gelsey torres > file = os.path.split(filename)[2]
gelsey torres > file += zlib.decompressobj()
gelsey torres >
Here is the catch of the Exception missing.
gelsey torres > def main():
gelsey torres > address = raw_input("Enter the address of the site: ")
gelsey torres > url, filename = os.path.split(address) # remove path and keep the
gelsey torres > name of the original file
gelsey torres > get_file(address, filename, url)
gelsey torres > if fnmatch(filename, "*.zip"):
gelsey torres > unzip_file(filename)
gelsey torres > else:
gelsey torres > pass
gelsey torres >
gelsey torres > main()
gelsey torres > response = raw_input("Do you want to run the program again? (y/n): ")
gelsey torres > while (response == "y"):
gelsey torres > main()
gelsey torres > else:
gelsey torres > raw_input("\n\nPress enter to quit.")
gelsey torres >
gelsey torres > When I ran it to test main(), I got this error:
gelsey torres >
gelsey torres > File "file_downloader.py", line 25
gelsey torres > def main():
gelsey torres > ^
gelsey torres > SyntaxError: invalid syntax
gelsey torres >
gelsey torres > Although sometimes it takes me awhile, I can usually spot the errors
gelsey torres > that I've made when they come up. I've stared at this one for two days
gelsey torres > and I still don't see the syntax error the interpreter is referring
gelsey torres > to. What am I doing wrong? I'm using Python 2.3 on Mac OS X version
gelsey torres > 10.3.9, and I run my scripts from Terminal.
gelsey torres >
gelsey torres > Thank you for your consideration,
gelsey torres >
gelsey torres > Gelsey
gelsey torres > _______________________________________________
gelsey torres > Tutor maillist - Tutor at python.org
gelsey torres > http://mail.python.org/mailman/listinfo/tutor
gelsey torres >
------------------- end ----------------------
The exception catching should solve your problem.
HTH Ewald
More information about the Tutor
mailing list