python script to find a file in directory 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 os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
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")