Pythonic way to check if a file exists?

Juanjo Conti picture Juanjo Conti · Feb 13, 2010 · Viewed 278.8k times · Source

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

Answer

Desintegr picture Desintegr · Feb 13, 2010

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.