[Tutor] Tutor Digest, Vol 78, Issue 8
Daniel Sarmiento
dsarmientos at gmail.com
Sun Aug 1 23:41:11 CEST 2010
> Date: Sun, 1 Aug 2010 12:28:49 -0700
> From: "Richard D. Moores" <rdmoores at gmail.com>
> To: python at bdurham.com
> Cc: tutor at python.org
> Subject: Re: [Tutor] How to get script to detect whether a file
> exists?
> Message-ID:
> <AANLkTinToAKh1Ag-jPtw2ybmZDH9fjrq5OHFp7qn5e2S at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Sun, Aug 1, 2010 at 11:33, <python at bdurham.com> wrote:
> > Richard,
> >
> > Look at the os.path.isfile function.
> >
> > Malcolm
>
> Thanks, Malcolm. That makes a tremendous difference.
>
> If anyone's curious, they see what use I've made of
> os.path.isfile(path): <http://tutoree7.pastebin.com/39kXSEjb>
>
> Dick
>
>
> >
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 78, Issue 8
> ************************************
>
I don't know if I am right, but I remember reading it is better to try to
open the files and see if an exception is raised than checking if the files
exist. In that way one can avoid TOCTTOU bugs (
http://en.wikipedia.org/wiki/Time-of-check-to-time-of-use )
Without knowing what your code does, I think it will be something like:
try:
F_unused = open(path1, 'rb')
F_used = open(path2, 'rb')
except IOError:
print("no")
.....
else:
unused_ints = pickle.load(F_unused)
......
HTH
Daniel Sarmiento S.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100801/5c41b547/attachment-0001.html>
More information about the Tutor
mailing list