What you are looking at can be done easily by using isfile
function of os.path
.
Official documentation of os.path.isfile
- Return True if path refers to a directory entry that is a symbolic link. Always False if symbolic links are not supported by the Python runtime.
It can be done as follows -
>>> import os
>>> os.path.isfile
<function isfile at 0x0205B1F0>
>>> os.path.isfile('C:\Python27\README.txt')
True
>>> os.path.isfile('README.txt')
True
>>> os.path.isfile('./README.txt')
True
>>> os.path.isfile('FAKENAME.txt')
False
Above code is the output of python.exe and there is a file README.txt adjacent to it.
Second command is just to show that os.path.isfile is a function.
We can pass the complete path or the absolute path to this function(as show in command 3, 4 and 5).