Fix Python – Pythonic way to check if a file exists? [duplicate]

Question

Asked By – Juanjo Conti

Which is the preferred way to check if a file exists and if not create it?

Now we will see solution for issue: Pythonic way to check if a file exists? [duplicate]


Answer

To check if a path is an existing file:

os.path.isfile(path)

Return True if path is an existing
regular file. This follows symbolic
links, so both islink() and
isfile() can be true for the same
path.

This question is answered By – Desintegr

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0