how to get the files in a directory in python code example
Example 1: get directory of file python
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
Example 2: get files in directory python
import os
files_and_directories = os.listdir("path/to/directory")
Example 3: list of files in python
import os
def fn():
file_list=os.listdir(r"C:\Users")
print (file_list)
fn()
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")