Re: [Tutor] AttributeError: 'str' object has no attribute, 'geturl'
> You are welcome. This time I would like to help you but the code is > incomplete > (import error for Image) and I have never used urllib2 so I don't really know > what to > do. > Again, try to debug it with pdb. Place "import pdb; pdb.set_trace()" where > you want > the break point and see what's going on. > > Javier Hi, I'm embarased to say that I was trying to download an image that doesn't exist (I had the url wrong). Thanks for hte tip on pdb, Joe ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'str' object has no attribute 'geturl'
Joseph Quigley wrote: > Ok, new version (sorry to bug you). > This time I've edited the program so that you can only download todays > (it won't work for any other date). Now I can connect to the server but > it sticks on "Downloading image!" > > Thanks for your prevoius help. > Joe You are welcome. This time I would like to help you but the code is incomplete (import error for Image) and I have never used urllib2 so I don't really know what to do. Again, try to debug it with pdb. Place "import pdb; pdb.set_trace()" where you want the break point and see what's going on. Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'str' object has no attribute 'geturl'
Ok, new version (sorry to bug you). This time I've edited the program so that you can only download todays (it won't work for any other date). Now I can connect to the server but it sticks on "Downloading image!" Thanks for your prevoius help. Joe #! /usr/bin/env python ## # Created by Joseph Quigley # # This program is under the GNU GPL Licence # # Either version 2 or any higher version. # # Garfield and the Garfield trade mark are # # Copyrighted by Paws Inc. # ## # Import modules import time import urllib2 import os import sys from Tkinter import * import Image import ImageTk import getpass class Data: # Define time and date todays_date = time.localtime(time.time()) todayDay = time.strftime("%d", todays_date) todayMonth = time.strftime("%m", todays_date) todayYear = time.strftime("%y", todays_date) todayYearFull = time.strftime("%Y", todays_date) getDate = None month = None day = None yearFull = None year = None # More file location junk stripName ="http://images.ucomics.com/comics/ga/2005"# % (yearFull) todayStrip = "ga051009.gif"#ga%s%s%s.gif" % (todayYear, todayMonth, todayDay) otherStrip = None download = None f = None VERSION = '0.9.3' try: if sys.argv[1] == "--version": print """\nGacor %s A Garfield Comic Downloader and reader. Copyright (C) 2005 Joseph Quigley This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n""" % Data.VERSION raise SystemExit except IndexError: print "\n" * 100 user = getpass.getuser() name = 'gacor' if os.name == 'posix': imgDir = '/home/%s/.gacor/' % user elif os.name == 'nt': if os.path.exists('C:\Documents and Settings'): imgDir = "C:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('D:\Documents and Settings'): imgDir = "D:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('E:\Documents and Settings'): imgDir = "E:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('F:\Documents and Settings'): imgDir = "F:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('G:\Documents and Settings'): imgDir = "G:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('H:\Documents and Settings'): imgDir = "H:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('I:\Documents and Settings'): imgDir = "I:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('J:\Documents and Settings'): imgDir = "J:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('K:\Documents and Settings'): imgDir = "K:\\Documents and Settings\\%s\\.gacor\\" % user else: setImgDir = raw_input("Please specify your Documents and Settings Drive\n(eg: C:\\ not C)\n> ") imgDir = "%sDocuments and Settings\\%s\\.gacor\\" % user if not os.path.exists(imgDir): os.mkdir(imgDir) def Connect(): print "Connecting to server..." try: Data.f = urllib2.urlopen("%s/%s" % (Data.stripName, Data.todayStrip)) except urllib2.URLError: print """Temporary failure in name resolution. This means that you may not be online or the site is down. You may want to try again later.\n\n This could also mean that you have entered a bad date in gettoher.""" print "\n\nProgram will quit in 3 seconds." time.sleep(3) raise SystemExit print "Connected." def Dsp_Image(pic): root = Tk() root.title("GaCoR (Garfield Comic Reader)") app = Frame(root) app.grid() img = Image.open(pic) imgPrep = ImageTk.PhotoImage(img) imgShow = Label(app, image=imgPrep).grid() info = Label(app, text="Displaying a Garfield for %s\%s\%s." % (Data.month, Data.day, Data.year)).grid() app.mainloop() def getImg(pic): print "Dowloading Image" Data.f.geturl() pict_Data = Data.f.read() pict = file("%s" % (pic), 'w') pict.write(pict_Data) pict.close() print "Finished Download" def ShowImg(pic): Dsp_Image("%s%s" % (imgDir, pic)) def comicDate(): while True: print "\nEnter comic date in mmdd format (eg: 09122002 "
Re: [Tutor] AttributeError: 'str' object has no attribute 'geturl'
Javier wrote: > Class Data has a class attribute, 'f', defined as an empty string. Check if > Data.f is initialized before calling getImg. > Also, why initialize with an empty str? Put a None in there or don't define > the attribute at all. > A tip: in line 126 there is a print Data.f. This will show nothing in this > case. When printing for debugging, error reporting or user input, always > place quotes around values so that it is easier to spot empty strings or non > printable characters. > Finally, keep in mind that you can always use pdb to see what's up. > >Javier > > Thanks, I'll try to clean up what had gotten past me. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] AttributeError: 'str' object has no attribute 'geturl'
Joseph Quigley wrote: > Well, I'm back to coding more on my comic downloader and viewer and I > keep getting this error: > > Traceback (most recent call last): > File "F:\Gacor\getgarf.py", line 244, in ? > getImg(Data.todayStrip) > File "F:\Gacor\getgarf.py", line 127, in getImg > Data.f.geturl() > AttributeError: 'str' object has no attribute 'geturl' > Class Data has a class attribute, 'f', defined as an empty string. Check if Data.f is initialized before calling getImg. Also, why initialize with an empty str? Put a None in there or don't define the attribute at all. A tip: in line 126 there is a print Data.f. This will show nothing in this case. When printing for debugging, error reporting or user input, always place quotes around values so that it is easier to spot empty strings or non printable characters. Finally, keep in mind that you can always use pdb to see what's up. Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] AttributeError: 'str' object has no attribute 'geturl'
Well, I'm back to coding more on my comic downloader and viewer and I keep getting this error: Traceback (most recent call last): File "F:\Gacor\getgarf.py", line 244, in ? getImg(Data.todayStrip) File "F:\Gacor\getgarf.py", line 127, in getImg Data.f.geturl() AttributeError: 'str' object has no attribute 'geturl' My code is attached (sorry, but it's got lot's of text) #! /usr/bin/env python ## # Created by Joseph Quigley # # This program is under the GNU GPL Licence # # Either version 2 or any higher version. # # Garfield and the Garfield trade mark are # # Copyrighted by Paws Inc. # ## # Import modules import time import urllib2 import os import sys from Tkinter import * import Image import ImageTk import getpass class Data: # Define time and date todays_date = time.localtime(time.time()) todayDay = time.strftime("%d", todays_date) todayMonth = time.strftime("%m", todays_date) todayYear = time.strftime("%y", todays_date) todayYearFull = time.strftime("%Y", todays_date) getDate = '' month = '' day = '' yearFull = '' year = '' # More file location junk stripName ="http://images.ucomics.com/comics/ga/%s/"; % (yearFull) todayStrip = "ga%s%s%s.gif" % (todayYear, todayMonth, todayDay) otherStrip = "" download = "" f = "" VERSION = '0.9.3' try: if sys.argv[1] == "--version": print """\nGacor %s A Garfield Comic Downloader and reader. Copyright (C) 2005 Joseph Quigley This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n""" % Data.VERSION raise SystemExit except IndexError: print "\n" * 100 user = getpass.getuser() name = 'gacor' if os.name == 'posix': imgDir = '/home/%s/.gacor/' % user elif os.name == 'nt': if os.path.exists('C:\Documents and Settings'): imgDir = "C:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('D:\Documents and Settings'): imgDir = "D:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('E:\Documents and Settings'): imgDir = "E:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('F:\Documents and Settings'): imgDir = "F:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('G:\Documents and Settings'): imgDir = "G:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('H:\Documents and Settings'): imgDir = "H:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('I:\Documents and Settings'): imgDir = "I:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('J:\Documents and Settings'): imgDir = "J:\\Documents and Settings\\%s\\.gacor\\" % user if os.path.exists('K:\Documents and Settings'): imgDir = "K:\\Documents and Settings\\%s\\.gacor\\" % user else: setImgDir = raw_input("Please specify your Documents and Settings Drive\n(eg: C:\\ not C)\n> ") imgDir = "%sDocuments and Settings\\%s\\.gacor\\" % user if not os.path.exists(imgDir): os.mkdir(imgDir) # Errors def Connect(): urllib2_URLError = """Temporary failure in name resolution. This means that you may not be online or the site is down. You may want to try again later.""" print "Connecting to server..." try: Data.f = urllib2.urlopen("%s%s" % (Data.stripName, Data.todayStrip)) except urllib2.URLError: print urllib2_URLError print "Connected." def Dsp_Image(pic): root = Tk() root.title("GaCoR (Garfield Comic Reader)") app = Frame(root) app.grid() img = Image.open(pic) imgPrep = ImageTk.PhotoImage(img) imgShow = Label(app, image=imgPrep).grid() info = Label(app, text="Displaying a Garfield for %s\%s\%s." % (Data.month, Data.day, Data.year)).grid() app.mainloop() def getImg(pic): print "Dowloading Image" print Data.f Data.f.geturl() pict_Data = Data.f.read() pict = file("%s%s" % (imgDir, pic), 'w') pict.write(pict_Data) pict.close() print "Finished Download" def ShowImg(pic): Dsp_Image("%s%s" % (imgDir, pic)) def comicDate(): while True: print "\nEnter comic date in mmdd format (eg: 09122002 "