[Tutor] Cannot fix OSError
jb
jb at riseup.net
Fri Jan 30 22:44:49 EST 2004
On Sat, Jan 31, 2004 at 01:39:41AM +0000, Nick Lunt wrote:
> 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.
you're not alone :p
> 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.
check out the man page for chmod: permission things are a bit different
for files and directory - to do what you want, you have to be able to read
the directory (you check os.R_OK for that) AND you have to be able to
enter and pass through it - os.X_OK is to be checked for that.
basically you can't tar a directory that doesnt have r and x fields set
for you.
i hope this helps
jb
More information about the Tutor
mailing list