[Tutor] how to know if a file exists
Andre Roberge
andre.roberge at gmail.com
Thu Jan 4 05:26:57 CET 2007
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/20070104/6ebb083c/attachment.htm
More information about the Tutor
mailing list