The simplest way, assuming you do not wish to go further down in the tree is:
import os
filepaths = []
iterdir = os.scandir(path_of_target_dir)
for entry in iterdir:
filepaths.append(entry.path)
Update:
A list comprehension makes is faster and more compact: (Strongly Recommended)
import os
iterdir = os.scandir(path_of_target_dir)
filepaths = [entry.path for entry in iterdir]
If you wish to filter by extension:
import os
iterdir = os.scandir(path_of_target_dir)
filepaths = [entry.path for entry in iterdir if entry.name.split('.')[-1] =='jpg'] # if you only want jpg files.
If you wish to filter by multiple extensions:
import os
iterdir = os.scandir(path_of_target_dir)
filepaths = [entry.path for entry in iterdir if entry.name.split('.')[-1] in {'jpg', 'docx'}] # if you only want jpg and docx files.
… or making it easier to read and modify and adding exclude filter:
import os
incl_ext = {'jpg', 'docx'} # set of extensions; paths to files with these extensions will be collected.
excl_ext = {'txt', 'bmp'} # set of extensions; paths to files with these extensions will NOT be collected.
get_ext = lambda file: file.name.split('.')[-1] # lambda function to get the file extension.
iterdir = os.scandir(path_of_target_dir)
filepaths = [entry.path for entry in iterdir if get_ext(entry) in incl_ext and get_ext(entry) not in excl_ext]
print(filepaths)
You can make it into a function. (You SHOULD make it into a function).
solved Python, I want to find file each subfolder on a folder