Use the Os.path.exists () method to directly determine whether a file exists.
The code is as follows:
>>> Import OS
>>> os.path.exists (R ' C:\1.TXT ')
False
Os.path.exists (PATH)
Return True If path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function could return False if permission is not granted to execute Os.stat () on the requested file, Even if the path physically exists.
Os.path.lexists (PATH)
Return True If path refers to an existing path. Returns True for broken symbolic links. Equivalent to exists () on platforms Lackingos.lstat ().
The main difference is that exists () automatically determines the invalid file link. If the checked file is a soft link, but the file that the soft connection points to is deleted, it returns false. and lexists () does not do this check, as long as the soft connection exists, even if the file it points to does not exist, it also returns true.
The difference between os.path.exists (path) and os.path.lexists (path)