Как получить все файлы в папке python
Перейти к содержимому

Как получить все файлы в папке python

  • автор:

Получение списка файлов в директории на Python

Всё чаще современные программисты предпочитают работать с языком программирования Python, потому что он очень гибкий, позволяющий легко взаимодействовать с операционной системой. Он также поставляется с функциями по работе с файловой системой. Решение задачи распечатки списка файлов в директории можно решить используя разные модули: os, subprocess, fnmatch и pathlib. Следующие решения демонстрируют, как успешно воспользоваться этими модулями.

Применение os.walk()

Модуль os содержит длинный список методов, которые касаются работы с файловой системой и операционной системой. Один из них walk(), возвращающий имена файлов в дереве каталогов, двигаясь по дереву сверху вниз или снизу вверх (сверху вниз по умолчанию).

os.walk() возвращает список из трех элементов: имя корневого каталога, список имен вложенных папок и список файлов в текущем каталоге. Он одинаково хорошо работает с интерпретаторами Python 2 и 3.

Использование командной строки, через subprocess

Модуль subprocess позволяет выполнить системную команду и собрать её результат. В нашем случае вызываемая системная команда выглядит следующим образом:

Инструкция ls -p . распечатывает список файлов текущего каталога, добавляя разделитель / в конце имени каждого подкаталога, которые нам понадобится на следующем шаге. Вывод этого вызова передается команде grep, которая отфильтровывает данные по мере поступления. Параметры -v / $ исключают все имена записей, которые заканчиваются разделителем /. Фактически / $ — регулярное выражение, которое соответствует всем строкам, содержащим символ / самым последним символом в строке, который определяется символом $.

Модуль subprocess позволяет строить настоящие конвейеры, а также соединять входные и выходные потоки, как это делается в командной строке. Вызов метода subprocess.Popen() открывает соответствующий процесс и определяет два параметра stdin и stdout.

Первая переменная ls определяет процесс выполнения ls –p для захвата stdout в конвейере. Поэтому поток stdout определяется как subprocess.PIPE. Вторая переменная grep также определяется как процесс, но вместо этого выполняет инструкцию grep –v /$.

Чтобы прочитать вывод команды ls из конвейера, поток stdin grep присваиваивается в ls.stdout. В заключение, переменная endOfPipe считывает вывод команды grep из grep.stdout, затем распечатывается в stdout циклом for.

Данное решение работает достаточно хорошо с Python 2 и 3, но его можно улучшить. Рассмотрим другие варианты.

Комбинация os и fnmatch

Решение, использующее подпроцессы, элегантно, но требует большого количества кода. Вместо этого, давайте объединим методы из двух модулей os и fnmatch. Этот вариант также работает с Python 2 и 3.

В качестве первого шага, импортируем модули os и fnmatch. Далее определим каталог, в котором нужно перечислить файлы, используя os.listdir(), а также шаблон для фильтрации файлов. В цикле for выполняется итерация списка записей, хранящихся в переменной listOfFiles.

В завершение, с помощью fnmatch отфильтровываются искомые записи и распечатываются соответствующие записи в stdout.

Использование os.listdir() и генераторов

Следующий вариант объединяет метод os.listdir() с функцией генератором. Код работает как с версиями 2, так и с 3 Python.

Как уже было сказано ранее, listdir() возвращает список записей для данного каталога. Метод os.path.isfile() возвращает True, если данная запись является файлом. Оператор yield завершает работу функции, но сохраняя текущее состояние и возвращает только имя записи являющейся файлом.

Использование pathlib

Модуль pathlib предназначен для парсинга, сборки, тестирования и иной работы с именами файлов и их путями, используя объектно-ориентированный API вместо низкоуровневых строковых операций. Начиная с Python 3 модуль находится в стандартной библиотеке.

В следующем листинге определяется текущий каталог точкой («.»). Затем метод iterdir() возвращает итератор, который возвращает имена всех файлов. Далее циклом for распечатываются имена файлов друг за другом.

В качестве альтернативы, можно отфильтровать файлы по именам с помощью метода glob. Таким образом, получаем требуемые файлы. Например, в приведенном ниже коде перечисляются Python файлы в выбранном каталоге, указав шаблон «*.py» в glob.

Использование os.scandir()

В Python 3.6 добавлен новый метод scandir(), доступный из модуля os. Как понятно из названия он значительно упрощает получение списка файлов в каталоге.

Чтобы определить текущий рабочий каталог и сохранить его, инициализируем значение переменной path, для этого импортируем модуль os и вызовем функцию getcwd(). Далее, scandir() возвращает список записей для выбранного пути, которые проверяются на принадлежность файлу, используя метод is_file().

Вывод

Ведутся споры, какой вариант является лучшим, какой наиболее элегантным и какой является наиболее «питоничным». Мне нравится простота метода os.walk(), а также модули fnmatch и pathlib.

Две версии с процессами/конвейером и итератором требуют более глубокого понимания процессов UNIX и знаний Python, поэтому они не могут быть предпочтительными для всех программистов из-за их дополнительной (и избыточной) сложности.

Чтобы найти ответ на этот вопрос, выберем самой быстрой из них, воспользовавшись удобным модулем timeit. Данный модуль подсчитывает время, прошедшее между двумя событиями.

Для сравнения всех решений без их изменений, воспользуемся функциональностью Python: вызовем интерпретатор с модулем timeit и соответствующим Python скриптом. Для автоматизации процесса напишем shell скрипт

Python List Files in a Directory

Also, there are multiple ways to list files in a directory. In this article, We will use the following four methods.

  • os.listdir(‘dir_path’) : Return the list of files and directories in a specified directory path.
  • os.walk(‘dir_path’) : Recursively get the list of all files in a directory and subdirectories.
  • os.scandir(‘path’) : Returns directory entries along with file attribute information.
  • glob.glob(‘pattern’) : glob module to list files and folders whose names follow a specific pattern.

Table of contents

How to List All Files in a Directory using Python

Use the listdir() and isfile() functions of an os module to list all files in a directory. Here are the steps.

    Import os module

First, import the os module. This module helps us to work with operating system-dependent functionality in Python. The os module provides functions for interacting with the operating system.

Next, decide the path to the directory you want to list the files of. Make sure you use the correct format for your operating system. For example, on Windows, paths are typically written with backslashes (e.g., ‘C:\\path\\to\\your\\directory’ ).

Now use the os.listdir(‘directory_path’) function to get a list of the names of the entries ( the files and directories ) in the directory given by the directory_path .

Next, Use for loop to Iterate the list returned by the os.listdir(directory_path) function. Using for loop we will iterate each entry returned by the listdir() function.

Now, In each loop iteration, use the os.path.isfile(‘path’) function to check whether the current entry is a file or a directory. If it is a file, then add it to a list. This function returns True if a given path is a file. Otherwise, it returns False.

Example: List Files in a Directory

Let’s see how to list all files in an ‘account’ directory.

Example 1: List only files in a directory

Output:

Here we got three file names.

Note:

  • The os.listdir(dir_path) will list files only in the current directory and ignore the subdirectories.
  • The os.path.join(directory, file_path) is used to get the full path of the entry.

Also, you should handle the case where the directory does not exist or an error occurs while accessing it. For this, you can use a try-except block. Here’s how you can modify the code:

Generator Expression:

If you know generator expression, you can make code smaller and simplers using a generator function as shown below.

Then simply call it whenever required.

Example 2: List both files and directories.

Directly call the listdir(‘path’) function to get the content of a directory.

Output:

As you can see in the output, ‘reports_2021’ is a directory.

os.walk() to list all files in a directory and subdirectories

Using the os.walk() function we can list all directories, subdirectories, and files in a given directory.

The os.walk() function returns a generator that creates a tuple of values (current_path, directories in current_path, files in current_path).

It is a recursive function, i.e., every time the generator is called, it will follow each directory recursively to get a list of files and directories until no further sub-directories are available from the initial directory.

For example, calling the os.walk(‘path’) will yield two lists for each directory it visits. The first list contains files, and the second list includes directories.

Let’s see the example of how to list all files in a directory and its subdirectories.

Example:

Output:

Note: You can also add a break statement inside a loop to stop looking for files recursively inside subdirectories.

Example:

os.scandir() To get the list of files in a directory

The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.

It returns an iterator of os.DirEntry objects, which contain file names.

Example:

Output:

Glob Module to list Files of a Directory

The Python glob module, part of the Python Standard Library, is used to find the files and folders whose names follow a specific pattern.

For example, to get all files of a directory, we will use the dire_path/*.* pattern. Here, *.* means file with any extension.

Let’s see how to list files from a directory using a glob module.

Example:

Output:

Note: If you want to list files from subdirectories, then set the recursive attribute to True.

Example:

Output:

Pathlib Module to list files of a directory

From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions.

  • Import pathlib module: Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems.
  • Next, Use the pathlib.Path(‘path’) to construct a directory path
  • Next, Use the iterdir() to iterate all entries of a directory
  • In the end, check if a current entry is a file using the path.isfile() function

Example:

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Related Tutorial Topics:

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

Работа с файлами в Python с помощью модуля OS

Обработка файлов в Python с помощью модуля os включает создание, переименование, перемещение, удаление файлов и папок, а также получение списка всех файлов и каталогов и многое другое.

В индустрии программного обеспечения большинство программ тем или иным образом обрабатывают файлы: создают их, переименовывают, перемещают и так далее. Любой программист должен обладать таким навыком. С этим руководством вы научитесь использовать модуль os в Python для проведения операций над файлами и каталогами вне зависимости от используемой операционной системы.

Важно знать, что модуль os используется не только для работы с файлами. Он включает массу методов и инструментов для других операций: обработки переменных среды, управления системными процессами, а также аргументы командной строки и даже расширенные атрибуты файлов, которые есть только в Linux.

Модуль встроенный, поэтому для работы с ним не нужно ничего устанавливать.

Вывод текущей директории

Для получения текущего рабочего каталога используется os.getcwd() :

os.getcwd() возвращает строку в Юникоде, представляющую текущий рабочий каталог. Вот пример вывода:

Создание папки

Для создания папки/каталога в любой операционной системе нужна следующая команда:

После ее выполнения в текущем рабочем каталоге тут же появится новая папка с названием «folder».

Если запустить ее еще раз, будет вызвана ошибка FileExistsError , потому что такая папка уже есть. Для решения проблемы нужно запускать команду только в том случае, если каталога с таким же именем нет. Этого можно добиться следующим образом:

Функция os.path.isdir() вернет True , если переданное имя ссылается на существующий каталог.

Изменение директории

Менять директории довольно просто. Проделаем это с только что созданным:

Еще раз выведем рабочий каталог:

Создание вложенных папок

Предположим, вы хотите создать не только одну папку, но и несколько вложенных:

Это создаст три папки рекурсивно, как показано на следующем изображении:

Создание вложенных папок

Создание файлов

Для создания файлов в Python модули не нужны. Можно использовать встроенную функцию open() . Она принимает название файла, который необходимо создать в качестве первого параметра и желаемый режим открытия — как второй:

w значит write (запись), a — это appending (добавление данных к уже существующему файлу), а r — reading (чтение). Больше о режимах открытия можно почитать здесь.

Переименование файлов

С помощью модуля os достаточно просто переименовать файл. Поменяем название созданного в прошлом шаге.

How to Get a List of All Files in a Directory With Python

Getting a list of all the files and folders in a directory is a natural first step for many file-related operations in Python. When looking into it, though, you may be surprised to find various ways to go about it.

When you’re faced with many ways of doing something, it can be a good indication that there’s no one-size-fits-all solution to your problems. Most likely, every solution will have its own advantages and trade-offs. This is the case when it comes to getting a list of the contents of a directory in Python.

In this tutorial, you’ll be focusing on the most general-purpose techniques in the pathlib module to list items in a directory, but you’ll also learn a bit about some alternative tools.

Source Code: Click here to download the free source code, directories, and bonus materials that showcase different ways to list files and folders in a directory with Python.

Before pathlib came out in Python 3.4, if you wanted to work with file paths, then you’d use the os module. While this was very efficient in terms of performance, you had to handle all the paths as strings.

Handling paths as strings may seem okay at first, but once you start bringing multiple operating systems into the mix, things get more tricky. You also end up with a bunch of code related to string manipulation, which can get very abstracted from what a file path is. Things can get cryptic pretty quickly.

Note: Check out the downloadable materials for some tests that you can run on your machine. The tests will compare the time it takes to return a list of all the items in a directory using methods from the pathlib module, the os module, and even the future Python 3.12 version of pathlib . That new version includes the well-known walk() function, which you won’t cover in this tutorial.

That’s not to say that working with paths as strings isn’t feasible—after all, developers managed fine without pathlib for many years! The pathlib module just takes care of a lot of the tricky stuff and lets you focus on the main logic of your code.

It all begins with creating a Path object, which will be different depending on your operating system (OS). On Windows, you’ll get a WindowsPath object, while Linux and macOS will return PosixPath :

  • Windows
  • Linux + macOS

With these OS-aware objects, you can take advantage of the many methods and properties available, such as ones to get a list of files and folders.

Note: If you’re interested in learning more about pathlib and its features, then check out Python 3’s pathlib Module: Taming the File System and the pathlib documentation.

Now, it’s time to dive into listing folder contents. Be aware that there are several ways to do this, and picking the right one will depend on your specific use case.

Getting a List of All Files and Folders in a Directory in Python

Before getting started on listing, you’ll want a set of files that matches what you’ll encounter in this tutorial. In the supplementary materials, you’ll find a folder called Desktop. If you plan to follow along, download this folder and navigate to the parent folder and start your Python REPL there:

Source Code: Click here to download the free source code, directories, and bonus materials that showcase different ways to list files and folders in a directory with Python.

You could also use your own desktop too. Just start the Python REPL in the parent directory of your desktop, and the examples should work, but you’ll have your own files in the output instead.

Note: You’ll mainly see WindowsPath objects as outputs in this tutorial. If you’re following along on Linux or macOS, then you’ll see PosixPath instead. That’ll be the only difference. The code you write is the same on all platforms.

If you only need to list the contents of a given directory, and you don’t need to get the contents of each subdirectory too, then you can use the Path object’s .iterdir() method. If your aim is to move through directories and subdirectories recursively, then you can jump ahead to the section on recursive listing.

The .iterdir() method, when called on a Path object, returns a generator that yields Path objects representing child items. If you wrap the generator in a list() constructor, then you can see your list of files and folders:

Passing the generator produced by .iterdir() to the list() constructor provides you with a list of Path objects representing all the items in the Desktop directory.

As with all generators, you can also use a for loop to iterate over each item that the generator yields. This gives you the chance to explore some of the properties of each object:

Within the for loop body, you use an f-string to display some information about each item.

In the second set of curly braces ( <> ) in the f-string, you use a conditional expression to print dir if the item is a directory, or file if it isn’t. To get this information, you use the .is_dir() method.

Putting a Path object in an f-string automatically casts the object to a string, which is why you no longer have the WindowsPath or PosixPath annotation.

Iterating over the object deliberately with a for loop like this can be very handy for filtering by either files or directories, as in the following example:

Here, you use a conditional statement and the .is_file() method to only print the item if it’s a file.

You can also place generators into comprehensions, which can make for very concise code:

Here, you’re filtering the resulting list by using a conditional expression inside the comprehension to check if the item is a directory.

But what if you need all the files and directories in the subdirectories of your folder too? You can adapt .iterdir() as a recursive function, as you’ll do later in the tutorial, but you may be better off using .rglob() , which you’ll get into next.

Recursively Listing With .rglob()

Directories are often compared with trees because of their recursive nature. In trees, the main trunk splits off into various main branches. Each main branch splits off into further sub-branches. Each sub-branch branches off itself too, and so on. Likewise, directories contain subdirectories, which contain subdirectories, which contain more subdirectories, on and on.

To recursively list the items in a directory means to list not only the directory’s contents, but also the contents of the subdirectories, their subdirectories, and so on.

With pathlib , it’s surprisingly easy to recurse through a directory. You can use .rglob() to return absolutely everything:

The .rglob() method with «*» as an argument produces a generator that yields all the files and folders from the Path object recursively.

But what’s with the asterisk argument to .rglob() ? In the next section, you’ll look into glob patterns and see how you can do more than just list all the items in a directory.

Using a Python Glob Pattern for Conditional Listing

Sometimes you don’t want all the files. There are times when you just want one type of file or directory, or perhaps all the items with a certain pattern of characters in their name.

A method related to .rglob() is the .glob() method. Both of these methods make use of glob patterns. A glob pattern represents a collection of paths. Glob patterns make use of wildcard characters to match on certain criteria. For example, the single asterisk * matches everything in the directory.

There are many different glob patterns that you can take advantage of. Check out the following selection of glob patterns for some ideas:

Glob Pattern Matches
* Every item
*.txt Every item ending in .txt , such as notes.txt or hello.txt
. Every item whose name is six characters long, such as 01.txt , A-01.c , or .zshrc
A* Every item that starts with the character A, such as Album , A.txt , or AppData
[abc][abc][abc] Every item whose name is three characters long but only composed of the characters a, b, and c, such as abc , aaa , or cba

With these patterns, you can flexibly match many different types of files. Check out the documentation on fnmatch , which is the underlying module governing the behavior of .glob() , to get a feel for the other patterns that you can use in Python.

Note that on Windows, glob patterns are case-insensitive, because paths are case-insensitive in general. On Unix-like systems like Linux and macOS, glob patterns are case-sensitive.

Conditional Listing Using .glob()

The .glob() method of a Path object behaves in much the same way as .rglob() . If you pass the «*» argument, then you’ll get a list of items in the directory, but without recursion:

Using the .glob() method with the «*» glob pattern on a Path object produces a generator that yields all the items in the directory that’s represented by the Path object, without going into the subdirectories. In this way, it produces the same result as .iterdir() , and you can use the resulting generator in a for loop or a comprehension, just as you would with iterdir() .

But as you already learned, what really sets the glob methods apart are the different patterns that you can use to match only certain paths. If you only wanted paths that ended with .txt , for example, then you could do the following:

Since this directory only has one text file, you get a list with just one item. If you wanted to get only items that start with real, for example, then you could use the following glob pattern:

This example also only produces one item, because only one item’s name starts with the characters real . Remember that on Unix-like systems, glob patterns are case-sensitive.

Note: The name here is referred to as the last part of the path, not the other parts of the path, which in this case would start with Desktop .

You can also get the contents of a subdirectory by including its name, a forward slash ( / ), and an asterisk. This type of pattern will yield everything inside the target directory:

In this example, using the «realpython/*» pattern yields all the files within the realpython directory. It’ll give you the same result as creating a path object representing the Desktop/realpython path and calling .glob(«*») on it.

Next up, you’ll look a bit further into filtering with .rglob() and learn how it differs from .glob() .

Conditional Listing Using .rglob()

Just the same as with the .glob() method, you can adjust the glob pattern of .rglob() to give you only a certain file extension, except that .rglob() will always search recursively:

By adding .md to the glob pattern, now .rglob() produces only .md files across different directories and subdirectories.

You can actually use .glob() and get it to behave in the same way as .rglob() by adjusting the glob pattern passed as an argument:

In this example, you can see that the call to .glob(«**/*.md») is equivalent to .rglob(*.md) . Likewise, a call to .glob(«**/*») is equivalent to .rglob(«*») .

The .rglob() method is a slightly more explicit version of calling .glob() with a recursive pattern, so it’s probably better practice to use the more explicit version instead of using recursive patterns with the normal .glob() .

Advanced Matching With the Glob Methods

One of the potential drawbacks with the glob methods is that you can only select files based on glob patterns. If you want to do more advanced matching or filter on the attributes of the item, then you need to reach for something extra.

To run more complex matching and filtering, you can follow at least three strategies. You can use:

  1. A for loop with a conditional check
  2. A comprehension with a conditional expression
  3. The built-in filter() function

In these examples, you’ve first called the .rglob() method with the «*» pattern to get all the items recursively. This produces all the items in the directory and its subdirectories. Then you use the three different approaches listed above to filter out the items that aren’t files. Note that in the case of filter() , you’ve used a lambda function.

The glob methods are extremely versatile, but for large directory trees, they can be a bit slow. In the next section, you’ll be examining an example in which reaching for more controlled iteration with .iterdir() may be a better choice.

Opting Out of Listing Junk Directories

Say, for example, that you wanted to find all the files on your system, but you have various subdirectories that have lots and lots of subdirectories and files. Some of the largest subdirectories are temporary files that you aren’t interested in.

For example, examine this directory tree that has junk directories—lots of them! In reality, this full directory tree is 1,850 lines long. Wherever you see an ellipsis ( . ), that means that there are hundreds of junk files at that location:

Large directory with junk subdirectories Show/Hide

The issue here is that you have junk directories. The junk directories are sometimes called temp , sometimes temporary files , and sometimes logs . What makes it worse is that they’re everywhere and can be at any level of nesting. The good news is that you don’t have to list them, as you’ll learn next.

Using .rglob() to Filter Whole Directories

If you use .rglob() , you can just filter out the items once they’re produced by .rglob() . To properly discard paths that are in a junk directory, you can check if any of the elements in the path match with any of the elements in a list of directories to skip:

Here, you’re defining SKIP_DIRS as a list that contains the strings of the paths that you want to exclude.

A call to .rglob() with a bare asterisk as an argument will produce all the items, even those in the directories that you aren’t interested in. Because you have to go through all the items, there’s a potential issue if you only look at the name of a path:

Since the name is just 0.txt , it wouldn’t match any items in SKIP_DIRS . You’d need to check the whole path for the blocked name.

You can get all the elements in the path with the .parts attribute, which contains a tuple of all the elements in the path:

Then, all you need to do is to check if any element in the .parts tuple is in the list of directories to skip.

You can check if any two iterables have an item in common by taking advantage of sets. If you cast one of the iterables to a set, then you can use the .isdisjoint() method to determine whether they have any elements in common:

If the two sets have no elements in common, then .isdisjoint() returns True . If the two sets have at least one element in common, then .isdisjoint() returns False . You can incorporate this check into a for loop that goes over all the items returned by .rglob(«*») :

In this example, you print all the items in large_dir that aren’t in any of the junk directories.

To check if the path is in one of the unwanted folders, you cast item.parts to a set and use .isdisjoint() to check if SKIP_DIRS and .parts don’t have any items in common. If that’s the case, then the item gets printed.

You can also accomplish the same effect with filter() and comprehensions, as below:

These methods are already getting a bit cryptic and hard to follow, though. Not only that, but they aren’t very efficient, because the .rglob() generator has to produce all the items so that the matching operation can discard that result.

You can definitely filter out whole folders with .rglob() , but you can’t get away from the fact that the resulting generator will yield all the items and then filter out the undesirable ones, one by one. This can make the glob methods very slow, depending on your use case. That’s why you might opt for a recursive .iterdir() function, which you’ll explore next.

Creating a Recursive .iterdir() Function

In the example of junk directories, you ideally want the ability to opt out of iterating over all the files in a given subdirectory if they match one of the names in SKIP_DIRS :

In this module, you define a list of strings, SKIP_DIRS , that contains the names of directories that you’d like to ignore. Then you define a generator function that uses .iterdir() to go over each item.

The generator function uses the type annotation : pathlib.Path after the first argument to indicate that you can’t just pass in a string that represents a path. The argument needs to be a Path object.

If the item name is in the exclude list, then you just move on to the next item, skipping the whole subdirectory tree in one go.

If the item isn’t in the list, then you yield the item, and if it’s a directory, you invoke the function again on that directory. That is, within the function body, the function conditionally invokes the same function again. This is a hallmark of a recursive function.

This recursive function efficiently yields all the files and directories that you want, excluding all that you aren’t interested in:

Crucially, you’ve managed to opt out of having to examine all the files in the undesired directories. Once your generator identifies that the directory is in the SKIP_DIRS list, it just skips the whole thing.

So, in this case, using .iterdir() is going to be far more efficient than the equivalent glob methods.

In fact, you’ll find that .iterdir() is generally more efficient than the glob methods if you need to filter on anything more complex than can be achieved with a glob pattern. However, if all you need to do is to get a list of all the .txt files recursively, then the glob methods will be faster.

Check out the downloadable materials for some tests that demonstrate the relative speed of different ways to list files in Python:

Source Code: Click here to download the free source code, directories, and bonus materials that showcase different ways to list files and folders in a directory with Python.

With that information under your belt, you’ll be ready to select the best way to list the files and folders that you need!

Conclusion

In this tutorial, you’ve explored the .glob() , .rglob() , and .iterdir() methods from the Python pathlib module to get all the files and folders in a given directory into a list. You’ve covered listing the files and folders that are direct descendants of the directory, and you’ve also looked at recursive listing.

In general, you’ve seen that if you just need a basic list of the items in the directory, without recursion, then .iterdir() is the cleanest method to use, thanks to its descriptive name. It’s also more efficient at this job. If, however, you need a recursive list, then you’re best to go with .rglob() , which will be faster than an equivalent recursive .iterdir() .

You’ve also examined one example in which using .iterdir() to list recursively can produce a huge performance benefit—when you have junk folders that you want to opt out of iterating over.

In the downloadable materials, you’ll find various implementations of methods to get a basic list of files from both the pathlib and os modules, along with a couple scripts that time them all against one another:

Source Code: Click here to download the free source code, directories, and bonus materials that showcase different ways to list files and folders in a directory with Python.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *