newbie permission problem

Peter Abel PeterAbel at gmx.net
Mon Oct 4 14:09:29 CEST 2004


"Ann" <Anna at nospam.invalid> wrote in message news:<X9Y7d.163228$MQ5.23824 at attbi_s52>...
> "Peter Abel" <PeterAbel at gmx.net> wrote in message

  ...
  ...
  ... [snip]

> >
> > The following works for me though it's a long way round:
> > >>> jpeg='filename.jpg'
> > >>> import cStringIO
> > >>> im=Image.open(cStringIO.StringIO(file(jpeg,'rb').read()))
> > >>> os.remove(jpeg)
> > >>>
> >
> > I had expected that the following would work too, but it doesn't:
> > >>> im=Image.open(file(jpeg,'rb'))
> > >>> os.remove(jpeg)
> > Traceback (most recent call last):
> >   File "<interactive input>", line 1, in ?
> > OSError: [Errno 13] Permission denied: 'filename.jpg'
> >
> > My opinion is that Image.open( filepointer ) binds the filpointer-object
> > to a name while StringIO doesn't and the garbagecollector will close the
>  file.
> >
> > BTW why do you open an imagefile when you want to delete it? If you want
>  to
> > decide by view which imagefile should be deleted it would be good practice
> > to open the imagefile, load the image and then close the file.
> >
> > Regards
> > Peter
> 
> Peter, I only want to find out the dimensions of the file.
> I have a huge number of jpg files and I want to automatically
> delete the tiny ones.
> Tnx for help.

OK, for this there is no need to open the files. 
The following function should do it:

>>> def delete_files(path,file_mask,limit_size):
... 	files=map(lambda lfn:\
... 	         (os.path.getsize(lfn),lfn),\
... 	         glob.glob(os.path.join(path,file_mask))\
... 	         )
... 	for (s,f) in files:
... 		if s < limit_size:
... 			print 'os.remove(%s): size: %d' % (f,s)
... 

>>> delete_files('.','*.py',200000)
os.remove(.\quiet.py): size: 1339
os.remove(.\Client.py): size: 923
os.remove(.\show_vblpc.py): size: 5411
os.remove(.\tcpBetriebsbild.py): size: 37737
>>> 


Regards
Peter



More information about the Python-list mailing list