Python 3.6’s New ‘scandir()’ Function

6’s newest addition scandir() function. This bad boy is a game changer for all you directory traversers out there. Say goodbye to the old-school os.walk() method and hello to this new, faster, more efficient way of navigating through directories.

So what exactly does it do? Well, scandir() creates an object that allows us to iterate over a directory’s contents in a memory-efficient manner. It returns a DirectoryIterator object which can be used with the for loop or list comprehension. This means we no longer have to load all of the files and directories into memory at once, making it perfect for large directories.

Here’s an example:

# Import the necessary libraries
import os # Importing the os library to access operating system functionalities
from pathlib import Path # Importing the Path class from the pathlib library to work with file paths

# Loop through the files in the specified directory with the .glob() method
for file in Path('path/to/directory').glob('*.txt'): # Using the .glob() method to iterate through all files with the .txt extension in the specified directory
    print(file) # Printing the file path of each file in the directory

This is equivalent to using scandir() and a list comprehension. But with scandir(), we can do it without loading all of the files into memory at once:

# Import the necessary libraries
import os # Importing the os library to access the operating system functionalities
from pathlib import Path # Importing the Path class from the pathlib library to work with file paths
from typing import Iterator # Importing the Iterator class from the typing library to specify the return type of the function

# Define a function to get all the files with the .txt extension in a given path
def get_files(path: str) -> Iterator[Path]: # Function definition with path as input parameter and Iterator[Path] as return type
    for entry in os.scandir(path): # Looping through the entries in the given path using os.scandir() function
        if entry.is_file() and entry.suffix == '.txt': # Checking if the entry is a file and has .txt extension
            yield Path(entry.name) # Yielding the file name as a Path object

# The above function is equivalent to the following list comprehension:
# [Path(entry.name) for entry in os.scandir(path) if entry.is_file() and entry.suffix == '.txt']
# However, using os.scandir() allows us to avoid loading all the files into memory at once, making it more memory efficient.

As you can see, scandir() is a much more efficient way of traversing directories. It’s also worth mentioning that it has some other cool features like being able to filter out certain file types or only iterate over directories (not files).

The new and improved scandir() function in Python 3.6. Say goodbye to the old-school os.walk() method and hello to this faster, more efficient way of navigating through directories.

SICORPS