[Tutor] Cannot fix OSError
Nick Lunt
nick at javacat.f2s.com
Fri Jan 30 20:39:41 EST 2004
Hi Folks,
this is my first posting to the list, although I have been snooping for
a couple of weeks ;)
I've recently started to learn Python and so far I'm having great fun.
Anyway, here's my problem, first off here's the program:
# code
import tarfile, sys, os
if len(sys.argv) <= 2:
print 'Error:', sys.argv[0], ' <tarfile.tar> <list of files>
sys.exit(1)
tfile = sys.argv[1]
totar = []
for f in sys.argv[2:]:
if os.access(f, os.R_OK): # NOTE1
totar.append(f)
else:
print 'Could not access', f, 'to tar'
tf = tarfile.TarFile(tfile, 'w')
for f in totar:
tf.add(f)
tf.close
# end code
This works fine until it encounters a file that I am not allowed to access, in which case it gives me this error:
# error
Traceback (most recent call last):
File "./tar.py", line 31, in ?
tf.add(f)
File "/usr/lib/python2.3/tarfile.py", line 1220, in add
self.add(os.path.join(name, f), os.path.join(arcname, f))
File "/usr/lib/python2.3/tarfile.py", line 1220, in add
self.add(os.path.join(name, f), os.path.join(arcname, f))
File "/usr/lib/python2.3/tarfile.py", line 1219, in add
for f in os.listdir(name):
OSError: [Errno 13] Permission denied: '/etc/skel/tmp'
# end error
I could trap it in a try/except OSError block, but I'd like to know why the line NOTE1 does not simply cause the program to skip the unreadable file and continue ?
>From the python interpreter 'os.access('/file/i/cant/read', os.R_OK)' returns False so I expected my 'if' on line NOTE1 to sort that out.
If someone could just explain to me where I'm going wrong I'd be extremely greatful.
Many thanks
Nick.
More information about the Tutor
mailing list