search for file in folder python code example
Example 1: get files in directory python
import os
files_and_directories = os.listdir("path/to/directory")
Example 2: python get files in directory
from pathlib import Path
for txt_path in Path("/path/folder/directory").glob("*.txt"):
print(txt_path)
Example 3: how to search a file in windows 10 using python
import os
def find_files(filename, search_path):
result = []
for root, dir, files in os.walk(search_path):
if filename in files:
result.append(os.path.join(root, filename))
return result
print(find_files("smpl.htm","D:"))
Example 4: python find file in directory
import os
if os.path.isfile("test.dat"):
print("yes")
else:
print("no")
import os
if "test.dat" in os.listdir():
print("yes")
else:
print("no")