[Tutor] code improvement for beginner ?

Scott Oertel me at scottoertel.info
Wed Oct 12 01:19:22 CEST 2005


lmac wrote:

> ---------------
>
>The problem with downloading the images is this:
>
>-------------
>http://images.nfl.com/images/globalnav-shadow-gray.gif
>Traceback (most recent call last):
>  File "/home/internet/bin/nflgrab.py", line 167, in ?
>    urllib.urlretrieve(img,img[f:])
>  File "/usr/lib/python2.3/urllib.py", line 83, in urlretrieve
>    return _urlopener.retrieve(url, filename, reporthook, data)
>  File "/usr/lib/python2.3/urllib.py", line 216, in retrieve
>    tfp = open(filename, 'wb')
>IOError: [Errno 13] Permission denied: '/globalnav-shadow-gray.gif'
>-------------
>
>Is there any solution to know if i can download the image ?
>
>Thanks.
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>  
>
I used this in my previous program while having problems writing files.

from sys import argv
from os.path import dirname, join

try:
   tfp = open(join(dirname(argv[0]), filename), 'wb')
except IOError:
   print "Unable to write file, pick another directory"

this will save it into the directory that your program resides in.

you might want to check out this current lib doc, 
http://www.python.org/doc/current/lib/os-file-dir.html
it has a bunch of objects of the os module that are good for testing if 
a directory is writable









More information about the Tutor mailing list