How can I list the contents of a directory in Python?

ProgrammingPythonServer Side Programming

A computer's file system's directory is an organisational feature used to store and locate files. A tree of directories is created by organising directories hierarchically. There are parent-child relationships in directories. A folder can also be used to refer to a directory. Python has accumulated a number of APIs that can list the directory contents over time. Useful functions include Path.iterdir, os.scandir, os.walk, Path.rglob, and os.listdir.

We could need a list of files or folders in a given directory in Python. You can accomplish this in a number of ways.

OS module

A function that returns a list of the files or folders in a directory is available in the OS module of Python.

The files in a directory are listed using the Python operating system library. Every file and folder in a directory are listed by the Python os.listdir() method. The function os.walk() returns a list of all the files in the whole file tree.

There are several ways to list files in a directory using the Python OS library.

Getting the files and folders in a directory using os.listdir() will be covered in this article.

Using os.listdir() method

The os.listdir() method in Python displays a list of all the files and folders in the specified directory.The operating system adopts special entries like "." and ".." to traverse between various directories, but this method does not return these.

Additionally, files and folders above the first level of directories are not returned by os.listdir().In other words, nothing from the method's found subfolders is returned by os.listdir().The directory's file path, from which you want to retrieve the names of its files and folders, is the only parameter that the os.listdir() function accepts.

Syntax

os.listdir(path)

Example

Following is an example to list the contents of directory using os.listdir() method -

# importing the module import os # Providing the path of the directory path = 'D:\Work TP' # Retrieving the list of all the files folders = os.listdir(path) # loop to iterate every item in the list for s in folders: print(s)

Output

Following is an output of the above code -

moving files.py
mysql_access.py
stored procedure python-sql.py
trial.py

Using the os.walk() method

A list of files contained within a tree can be obtained using the os.walk() function. Each directory in a tree is iterated over by the technique.

The names of all files and folders contained within a directory and any subdirectories are then returned by os.walk().

Syntax

os.walk(topdown)

topdown indicates that directories should be scanned from the top down when set to True. Directories will be scanned from the bottom up if this value is False (optional).

Example

Following is an example to list the contents of directory using os.walk() method:

# importing the module import os # Providing the path path = 'D:\Work TP' # loop for retrieving all the files and folders for top-down search for root, directories, contents in os.walk(path, topdown=False): # joining together the root folder where the directory exists of every files along with its name for name in contents: print(os.path.join(root, name)) for name in directories: print(os.path.join(root, name))

Output

Following is an output of the above code -

D:\Work TP\SubDirectory\How to copy files from one folder to another using Python.docx
D:\Work TP\SubDirectory\How to create an empty file using Python.docx
D:\Work TP\SubDirectory\Sarika Sample Articles (Python-MySQL Procedures).docx
D:\Work TP\SubDirectory\sql python create table.docx
D:\Work TP\moving files.py
D:\Work TP\mysql_access.py
D:\Work TP\stored procedure python-sql.py
D:\Work TP\trial.py
D:\Work TP\SubDirectory

Using the os.scandir() method

Scandir uses the same directory iteration system calls as listdir to obtain the names of the files on the specified path, but it differs from listdir in two areas.

  • The lightweight DirEntry objects are returned as opposed to bare filename strings, holding the filename string and offering easy methods to retrieve any additional data the operating system may have returned.
  • Instead of instantly returning the entire list, scandir behaves as a true iterator by returning a generator as contrasted to a list.

For each file and subdirectory in the path, scandir() returns a DirEntry object.

Example

Following is an example to list the contents of directory using os.scandir() method -

# importing the modules import os # getting all the files present inside a specific folder dir_track = r'D:\Work TP' for track in os.scandir(dir_track): if track.is_file(): print(track.name)

Output

Following is an output of the above code -

moving files.py
mysql_access.py
stored procedure python-sql.py
trial.py

glob module

The Python glob module allows us to search over all path names in order to find files that fit a given pattern . The rules established by the Unix shell are used to define the supplied pattern for file matching.

The output of the software returns the result acquired by following to these guidelines for a certain pattern file matching in the random order. Because the glob module can navigate through the list of files at a specific point in our local disc, we must meet certain requirements when using the file matching pattern. The module will primarily go over disc lists of files that only follow a particular pattern.

Using the glob() for searching files recursively

Pathname and recursive flag are the two parameters this function requires:

  • pathname − relative or absolute (including both the file name and the complete path) (with UNIX shell-style wildcards). By providing either an absolute or relative path to the glob() method, we may do a file search. A path name with a complete directory structure is known as an absolute path. A relative path is a pathname that comprises directory names as well as one or more wildcard characters.
  • recursive − If set to True, recursive file searches will be performed. It searches all files in the current directory's subdirectories recursively.The recursive flag's default setting is False. This means that it will only look in the folder that is listed in our search path.

It searches all files in the current directory's subdirectories recursively.The recursive flag's default setting is False. This means that it will only look in the folder that is listed in our search path.

Syntax

glob.glob(pathname, *, recursive=False)

Example

Following is an example to list the contents of directory using glob.glob() method using absolute path:

import glob # relative path for searching all the python files contents = glob.glob("D:\Work TP\*.py",recursive=True) print(contents)

Output

Following is an output of the above code -

['D:\Work TP\moving files.py', 'D:\Work TP\mysql_access.py', 'D:\Work TP\stored procedure python-sql.py', 'D:\Work TP\trial.py']

Using the iglob() to loop through the Files

The only difference between iglob() and glob() is that the former provides an iterator of file names that fit the pattern. The method generates an iterator object, which we can loop through to obtain the names of the individual files.

Syntax

glob.iglob(pathname,*,recursive=False)

Without actually storing all of the items together, return an iterator that produces the same values as glob().

When called, iglob() produces a callable object that loads the results into memory.

Example

Following is an example to list the contents of directory using glob.iglob() method -

# importing the module import glob # providing the path glob.iglob('D:\Work TP/*.py') # iterating over the files in that path of directory for items in glob.iglob('D:\Work TP/*.py'): print(items)

Output

Following is an output of the above code -

D:\Work TP\moving files.py
D:\Work TP\mysql_access.py
D:\Work TP\stored procedure python-sql.py
D:\Work TP\trial.py

Pathlib module

We can use the pathlib module, which offers a wrapper for most OS functions, starting with Python 3.4.

  • import pathlib− For many operating systems, the Pathlib module provides classes and methods to manage filesystem paths and retrieve file-related data.
  • Use the pathlib.Path('path') next for constructing the path of the directory.
  • Next, go through each entry in a directory using iterdir().
  • In the end, use the path.isfile() function to determine whether a current element is a file.

Example

Following is an example to list the contents of directory using pathlib module -

# importing the module import pathlib # path of the directory dir_path = r'D:\Work TP' # storing the file names res = [] # constructing the path object d = pathlib.Path(dir_path) # iterating the directory for items in d.iterdir(): # checking if it's a file if items.is_file(): res.append(items) print(res)

Output

Following is an output of the above code -

[WindowsPath('D:/Work TP/moving files.py'), WindowsPath('D:/Work TP/mysql_access.py'), WindowsPath('D:/Work TP/stored procedure python-sql.py'), WindowsPath('D:/Work TP/trial.py')]
raja
Updated on 17-Aug-2022 13:20:42

Advertisements