python get files in current directory code example
Example 1: python get current file location
import os
os.path.dirname(os.path.abspath(__file__))
Example 2: python list files in current directory
import os
files = os.listdir('.')
print(files)
for file in files:
Example 3: get file names in folder python
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
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")