import os
folder_path = "./folder-path"
for path, currentDirectory, files in os.walk(folder_path):
for file in files:
if not file.startswith("."):
print(os.path.join(path, file))
import os
# returns name of all files & directory exist in current location
files_dir = os.listdir('./blogs')
# './blogs' is the directory in the current location
print(files_dir)
only_files = []
for i in files_dir:
if os.path.isfile('./blogs/'+i):
only_files.append(i)
only_dir = []
for i in files_dir:
if os.path.isdir('./blogs/'+i):
only_dir.append(i)
print('-'*15)
print(only_files) # prints all files
print('-'*15)
print(only_dir) # prints all directories
"""
OUTPUT:
['1.txt', '2.txt', '3.txt', '4.txt', '5.txt', '6.txt', '7.txt', '8.txt', 'Test Directory 1', 'Test Directory 2']
---------------
['1.txt', '2.txt', '3.txt', '4.txt', '5.txt', '6.txt', '7.txt', '8.txt']
---------------
['Test Directory 1', 'Test Directory 2']
"""
"""Examples from SO (see link)
Obviously the directory can be any directory with 'read access' (not only `os.curdir`)
Doc about the `os` module: https://docs.python.org/3/library/os.html
"""
# /! Return a `filter` object, not a `list`
import os
files_ex1 = filter(os.path.isfile, os.listdir(os.curdir))
# List comprehension
files_ex2 = [f for f in os.listdir(os.curdir) if os.path.isfile(f)]