how to print the contents of a directory in python code example
Example 1: python list files in current directory
import os
files = os.listdir('.')
print(files)
for file in files:
Example 2: 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 3: store all files name in a folder python
import os
ALL_FILES_IN_DIR = [ELEM for ELEM in os.listdir() if "txt" in ELEM]
ALL_DIR = [ELEM for ELEM in os.listdir() if "." not in ELEM]
Example 4: python program to print the contents of a directory using os module
import os
li = os.listdir(".")
print(li)