Python – List all files and directories in a directory

List all files and directories in a directory

from os import listdir
my_path = '/home/jack'
files_list = listdir(my_path)
print(files_list)

List all files in a directory

from os import listdir
from os.path import isfile, join
my_path = '/home/jack'
files_list = [f for f in listdir(my_path) if isfile(join(my_path, f))]
print(files_list)

List all directories in a directory

from genericpath import isdir
from os import listdir
from os.path import isfile, join
my_path = '/home/jack'
files_list = [f for f in listdir(my_path) if isdir(join(my_path, f))]
print(files_list)