[Tutor] try and file existence
Steven D'Aprano
steve at pearwood.info
Sun Aug 16 01:41:34 CEST 2015
On Sat, Aug 15, 2015 at 02:24:21PM -0500, boB Stepp wrote:
> On Fri, Aug 14, 2015 at 10:39 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> > There is also os.path.exists(filename), but you should avoid using that
> > if possible. The problem is this:
> >
> > if os.path.exists(filename):
> > # file exists *right now*
> > # but a millisecond later, some other program deletes it...
> > # and now it doesn't exist any more
> > with open(filename) as f: # gives an error
> > ...
>
> I understand your points, but wonder then what is the intended use for
> os.path.exists()? That is, in what types of circumstances would it be
> both appropriate and safe to use?
def print_file_names(possible_names):
print("List of file names checked")
print("--------------------------"
for name in possible_names:
if os.path.exists(name):
print(name)
else:
print("missing:", name)
--
Steve
More information about the Tutor
mailing list