Listing Directory Using Python in two methods
Listing Directory Using Python in two methods

There are two modified ways or methods of the files contained in a specified also see PYTHON listing files contained in a directory which explains the detailed note in advance of using os.walker and glob with specified file type but here I described how to simply list files contained in a directory in two simple methods.

1st Method

method of Directory listing with

import os entries = os.listdir('shopinson/') for entry in entries:     print(entry)

the above python code snippets displays all files contained in the specified directory named “shopinson

2nd Method

Modern method of Directory listing with python

#1 Example: 2nd Method Using os.scandir()

import os with os.scandir('shopinson/') as entries:     for entry in entries:         print(entry.name) 

#2 Example: 2nd Method Using pathlib.Path()

from pathlib import Path  entries = Path('shopinson/') for entry in entries.iterdir():     print(entry.name)

Difference between the two method is that the 1st method uses os.listdir() while the second method uses os.scandir() and pathlib.Path().

Note: to get list of file types including it types best method is the modern method

LEAVE A REPLY

Please enter your comment!
Please enter your name here