I would like to find that whether there is any file exists in a specific directory or not with using python.
Something like this;
if os.path.exists('*.txt'):
# true --> do something
else:
# false --> do something
To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .
You can use glob.glob
with wildcards and check whether it returns anything. For instance:
import glob
if glob.glob('*.txt'):
# file(s) exist -> do something
else:
# no files found
Note that glob.glob
return a list of files matching the wildcard (or an empty list).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With