[Tutor] how to know if a file exists

shawn bright nephish at gmail.com
Thu Jan 4 05:29:39 CET 2007


thanks, luke, Andre.
appreciate it a lot

shawn

On 1/3/07, Andre Roberge <andre.roberge at gmail.com> wrote:
>
>
>
> On 1/4/07, Luke Paireepinart <rabidpoobear at gmail.com> wrote:
> >
> > shawn bright wrote:
> > > hello there,
> > > i am writing an app for linux. what command would be easiest to test
> > > and see if a certain file exist ?
> > > i was going to do something like this
> > > try:
> > >     file = open('/path/to/file', 'rb')
> > >     return True
> > > except:
> > >     return False
> > You should except IOError here, just to be clear and such.
> > >
> > > but i thought that there would be an easier way.
> > The os module has some function for checking if files exist, I think.
>
>
> Yes, check out access():
> http://docs.python.org/lib/os-file-dir.html
>
>  André
>
> Or you could do
> > if targetfile not in os.listdir(directory): return False
> > else: return True
> >
> > But your original solution is okay.
> > HTH,
> > -Luke
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070103/4be0f1e5/attachment.html 


More information about the Tutor mailing list