How to get all files in directory python
How to get all files in directory python
Python list directory, subdirectory, and files
I’m trying to make a script to list all directory, subdirectory, and files in a given directory.
I tried this:
Unfortunatly it doesn’t work properly.
I get all the files, but not their complete paths.
For example if the dir struct would be:
What I need is the first result. Any help would be greatly appreciated! Thanks.
12 Answers 12
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Use os.path.join to concatenate the directory and file name:
Note the usage of path and not root in the concatenation, since using root would be incorrect.
In Python 3.4, the pathlib module was added for easier path manipulations. So the equivalent to os.path.join would be:
The advantage of pathlib is that you can use a variety of useful methods on paths. If you use the concrete Path variant you can also do actual OS calls through them, like changing into a directory, deleting the path, opening the file it points to and much more.
Just in case. Getting all files in the directory and subdirectories matching some pattern (*.py for example):
Couldn’t comment so writing answer here. This is the clearest one-line I have seen:
Here is a one-liner:
Note: os.path.expanduser and/or os.path.expandvars can be used for paths strings like
Extending this example:
Its easy to add in file basename tests and directoryname tests.
For Example, testing for *.jpg files:
A bit simpler one-liner:
You can take a look at this sample I made. It uses the os.path.walk function which is deprecated beware.Uses a list to store all the filepaths
Since every example here is just using walk (with join ), i’d like to show a nice example and comparison with listdir :
So as you can see for yourself, the listdir version is much more efficient. (and that join is slow)
It’s just an addition, with this you can get the data into CSV format
Another option would be using the glob module from the standard lib:
If you need an iterator you can use iglob as an alternative:
Using any supported Python version (3.4+), you should use pathlib.rglob to recusrively list the contents of the current directory and all subdirectories:
If you want something copy-pasteable:
Example
Pretty simple solution would be to run a couple of sub process calls to export the files into CSV format:
That command produces comma separated values that can be easily analyzed in Excel.
The resulting CSV file doesn’t have a header row, but you can use a second command to add them.
Depending on how much data you get back, you can massage it further using Pandas. Here are some things I found useful, especially if you’re dealing with many levels of directories to look through.
Python List Files in a Directory
Updated on: January 19, 2022 | Leave a Comment
In this article, we will see how to list all files of a directory in Python. There are multiple ways to list files of a directory. In this article, We will use the following four methods.
Table of contents
How to List All Files of a Directory
Getting a list of files of a directory is easy as pie! Use the listdir() and isfile() functions of an os module to list all files of a directory. Here are the steps.
This module helps us to work with operating system-dependent functionality in Python. The os module provides functions for interacting with the operating system.
Use for loop to Iterate the files returned by the listdir() function. Using for loop we will iterate each file returned by the listdir() function
In each loop iteration, use the os.path.isfile(‘path’) function to check whether the current path is a file or 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 to List Files of a Directory
Let’s see how to list files of an ‘account’ folder. The listdir() will list files only in the current directory and ignore the subdirectories.
Example 1: List only files from a directory
Output:
Here we got three file names.
If you know generator expression, you can make code smaller and simplers using a generator function as shown below.
Generator Expression:
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 directory and subdirectories
The os.walk() function returns a generator that creates a tuple of values (current_path, directories in current_path, files in current_path).
Note: Using the os.walk() function we can list all directories, subdirectories, and files in a given directory.
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 to list all files in directory and subdirectories.
Example:
Output:
Note: Add break inside a loop to stop looking for files recursively inside subdirectories.
Example:
os.scandir() to get files of 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 contains 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.
Example:
Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.
About Vishal
Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!
Related Tutorial Topics:
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
How to List Files in a Directory Using Python?
In this tutorial, we’re covering everything you need to know about how to list files in a directory using Python.
Python is a general-purpose language, used in a variety of fields like Data Science, Machine Learning, and even in Web Development. There seems to be no restriction in the application of Python Language.
Therefore, it seems quite trivial Python can be used to list files and directories in any system. The aim of this article is to illuminate the reader about the ways to list files in a system using Python.
List All Files in a Directory Using Python
For the purpose of interacting with directories in a system using Python, the os library is used.
1. Using the ‘os’ library
The method that we are going to exercise for our motive is listdir(). As the name suggests, it is used to list items in directories.
Output:
Linux users can easily match the above output using the standard ls command on the terminal.
As we can see the outputs of each method matches.
2. Using the ‘glob’ library
glob is mostly a filename pattern matching library, but it can be used to list items in the current directory by:
Output:
The wildcard character ‘*’ is used to match all the items in the current directory. Since we wish to display the items of the current directory, we need to switch off the recursive nature of glob() function.
3. List only files in the current directory
In the above methods, the python code was returning all the items in the current directory irrespective of their nature. We can extract only the files using the path.isfile() function inside the os library.
Output:
In the above code snippet, List Comprehension is used to filter out only those items that are actually a file.
Another key thing to note here is that, the above code does not work for other directories as the variable ‘f’ is not an absolute path, but a relative path to the current directory.
List All Files in a Directory Recursively
In order to print the files inside a directory and its subdirectories, we need to traverse them recursively.
1. Using the ‘os’ library
With the help of the walk() method, we can traverse each subdirectory within a directory one by one.
Output:
The os.walk() method simply follows each subdirectory and extracts the files in a top-down manner by default. There are three iterators used for going through the output of os.walk() function:
The join() method is used to concatenate the file name with its parent directory, providing us with the relative path to the file.
2. Using the ‘glob’ library
Similar to the above procedure, glob can recursively visit each directory and extract all items and return them.
Output:
The ‘**’ symbol used along with the path variable tells the glob() function to match files within any subdirectory. The ‘*’ tells the function to match with all the items within a directory.
Since we wish to extract only the files in the complete directory, we filter out the files using the isfile() function used before.
List All Subdirectories Inside a Directory
Instead of listing files, we can list all the subdirectories present in a specific directory.
Output:
The minor difference between listing files and directories is the selection of iterator during the process of os.walk() function. For files, we iterate over the files variable. Here, we loop over the folders variable.
List Files in a Directory with Absolute Path
Once we know how to list files in a directory, then displaying the absolute path is a piece of cake. The abspath() method provides us with the absolute path for a file.
Output:
One thing to note here is that abspath() must be provided with the relative path of the file and that is the purpose of join() function.
List Files in a Directory by Matching Patterns
There are multiple ways to filter out filenames matching a particular pattern. Let us go through each of them one by one.
1. Using the ‘fnmatch’ library
As the name suggests, fnmatch is a filename pattern matching library. Using fnmatch with our standard filename extracting libraries, we can filter out those files matching a specific pattern.
Output:
The fnmatch() function takes in two parameters, the filename followed by the pattern to be matched. In the above code, we are looking at all the files containing the word file in them.
2. Using the ‘glob’ library
As we mentioned before, glob’s primary purpose is filename pattern matching.
Output:
The above pattern matching regular expression ‘**/*7*.*’ can be explained as:
3. Using the ‘pathlib’ library
pathlib follows an object-oriented way of interacting with the filesystem. The rglob() function inside the library can be used to recursively extract list of files through a certain Path object.
These list of files can be filtered using a pattern within the rglob() function.
Output:
List Files in a Directory with a Specific Extension
Listing files with a specific extension in Python is somewhat similar to pattern matching. For this purpose, we need to create a pattern with respect to the file extension.
Output:
This sums up the ways to fetch list of files in a directory using Python.
Conclusion
There can be multiple ways to solve any problem at hand, and the most convenient one is not always the answer. With respect to this article, a Python programmer must be aware of every way we can list files in a directory.
We hope this article was easy to follow. Feel free to comment below for any queries or suggestions.
Python get all files in directory + various examples
In this Python tutorial, we will learn Python get all files in directory and also we will cover these topics:
Python get all files in directory
Here, we can see how to list all files in a directory in Python.
All the files from the directories can be seen in the output. You can refer to below screenshot for the output.
This is how to get all files from a directory in Python.
Python list all files in directory with extension
Now, we can see how to list all files in a directory with extension in Python.
The file which is having a .txt extension is listed in the output. You can refer to the below screenshot for the output.
This is how to get all files in a directory with extension in Python.
Python get all files directories with a given range
Now, we can see how to list all files in the directories in the given range in python.
We can see the list of files with a filename which contains number in the range9. You can refer to the below screenshot for the output.
Python all files directories with a given range
This is how to get all files directories with a given range in Python.
List all directories with the given directories Python
Here, we can see how to list all directories with the given directories in python.
We can only see the directories from the given directory as the output. The below screenshot shows the output.
List all directories with the given directories python
This is how to list all directories with the given directories in Python.
Python list all files in directory recursively
Now, we can see how to list all filles in a directory recursively in python.
In the below screenshot we can see the list of files with .csv extension
python list all files in directory recursively
This is how to get all files in a directory recursively in Python.
Python all files in a directory to list
Here, we can see all files in a directory to list in python.
We can see the list of files from the directories. You can refer to the below screenshot for the output
Python delete all files from a directory
Here, we can see how to delete all files from a directory in python.
As all the files are removed, files are not displayed in the output. This is how we can delete all files from a directory in Python.
Python get all files in a directory that match a pattern
Here, we can see how to get all files in a directory that match a pattern in python
Here, we can see the files that match a pattern as new in the output. You can refer to the below screenshot for the output:
Python get all files in a directory starting with
Here, we can how to get all files in a directory starting with in python
We can see the names of files only starting with new in the output. You can refer to the below screenshot for the output.
Python get files in a directory without an extension
Here, we can see how to get files in a directory without an extension in python
We can only see the name of the file without extension in the output. Below screenshot shows the output.
Python get all files in directory filter
Now, we can see how to get all files in directory using filter in python
We can see all the files from the directory which are have only .txt extension as the output. You can refer to the below screenshot.
Python directory with the size
Here, we can see how to get files from directory with the size in python
In the below screenshot, we can see size in bytes and also in megabytes as the output.
Python list all files in a directory with an absolute path
Now, we can see how to list all files in a directory with an absolute path in python
We can see all the files from the absolute path as the output. You can refer to the below screenshot for the output.
Python list all files in a directory with an absolute path
Python list all files in a directory and subdirectory
Now, we can see how to list all files in a directory and subdirectory in python
The below screenshot show the output with all the files from directory and subdirectory.
Python list all files in a directory and subdirectory
Python list all files in subdirectories with extension
Here, we can see how to list all files in subdirectories with extension in python
We can see the list of files from the subdirectories as the output. You can refer to the below screenshot for the output.
Python get all files in directory full path
Here, we can see how to get all files in directory fullpath in python
We can see the list of files from the assigned fullpath in the output. You can refer to the below screenshot for the output.
Python list all files in directory and subdirectories with size
Here, we can see how to get files from directory and subdirectory with the size in python
In the below screenshot, we can see size in bytes and also in megabytes as the output.
Python list all files in directory and subdirectories with size
Python all files in directory filter to get jpg files
Now, we can see how to get all files in directory using filter to get jpg files in python
We can see all the files from the directory which are have only .jpg extension as the output. You can refer to the below screenshot.
You may like the following Python tutorials:
In this tutorial, we have learned about Python get all files in a directory, and also we have covered these topics:
Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.
How to List Files In Directory in python- Detailed Guide
Listing files in a directory is useful to check the files available in the directory.
You can list files in a directory in python using the os.listdir() method.
In this tutorial, you’ll learn how to list files in a directory in python using the different libraries.
If You’re in Hurry…
You can use the below code snippet to list files in a directory.
os.listdir() lists all the files and folders in the directory. If a path is not given, then it lists the files from the current working directory.
Use only forward slash in the directory path location.
Output
The highlighted object is the folder in the directory.
Listing Only Files in a Directory
To display only the files in a directory, you can use the os.walk() method.
It’ll return two separate lists of files and folders. You can iterate the files list and access the files in the directory.
Snippet
Output
If You Want to Understand Details, Read on…
In this tutorial, you’ll learn the different methods available to list files in a directory.
There are five methods available to List files in a directory. You’ll learn how to use these methods and also learn how to use these methods for different use-cases.
Table of Contents
Using listdir()
listdir fetches all files and folders in the directory.
It lists all files and folders in the current directory.
Snippet
Output
If you want to list files from a custom directory, then you can pass the directory name to the listdir() method as shown below.
Snippet
Output
This is how you can use the listdir() method.
Using os.walk()
os.walk() method can be used to list the files and folders in a directory.
This method walks through the directory either top-down or bottom-up and returns the list of the entries.
For more details, refer the doc.
Snippet
You can see the below output. The entries in the directory are printed in a top to bottom order.
Output
This is how you can use the os.walk() method.
Using os.scandir()
You can use the os.scandir() method to list the entries in the directory.
os.scandir() returns the iterator of the directory objects. Then it can be iterated and printed as follows. For more details, refer to the doc.
In the example, first, you’ll generate the iterator of the entries available in the directory and use a for loop to iterate the entries. And each entry name can be printed using the entry.name attribute.
Snippet
You’ll see all the available entries in the directory printed as below.
Output
This is how you can scan the directory and iterate the entries to print its names.
Using Pathlib
You can use the PathLib library to list all files from a directory. For more details, refer to the pathlib doc.
First, create an object using the directory from which you want to list files.
Then with the object, iterate the directory using the iterdir() method and print each file entry.
This will print all the files and just the sub-directories available in the main directory.
It’ll not print the files available in the sub-directories.
Snippet
You’ll see the file names and the subdirectory names.
Output
This is how you can use Pathlib to list the files in a directory.
Using glob
You can use the glob API to list files using Pattern matching or regular expressions. It is very useful when you don’t know the exact file names or directory names but you want to check if a file exists with such a pattern.
Snippet
Output
These are the methods you can use to get All Files In Directory.
Next, let’s discuss the use-cases.
List Files In Directory And Subdirectories
In this section, you’ll learn how to list all files in directories and subdirectories.
You can do this by using the os.walk() method. It walks through all the entries in a directory.
If any subdirectory is found, it also walks through the entries available in the subdirectory.
Finally, it yields separate tuples which contain directory paths, subdirectories, and files.
You can iterate over the files tuple to access the file entries in the directory.
Snippet
You’ll see all the files printed in the main directory and the subdirectory.
Output
This is how you can print the files in a directory and sub-directories.
List Files In Directory Full Path
In this section, you’ll learn how to list files in a directory with full path information. As similar to the previous section, you’ll walk through the directory using the os.walk() method and obtain separate tuples with path, sub-directory, and the files information.
To list the files with a full path, you can use the os.path.join() method to join the path and the name of the file as shown below.
Snippet
You’ll see the below output. Filenames will be printed with the full path information.
Output
This is how you can list files with full path information.
List Files In Directory With Pattern Matching
In this section, you’ll learn how to list files in a directory with pattern matching.
This will be useful when you don’t know the exact file name but want to find files in a specific pattern.
You can use the regex to list files in a directory.
For example, let’s learn how to list down the files starting with the name sample using the glob library.
Starting With
The regex for finding files starting with the text sample is sample*. This means the file name should start with the text sample and after that, it may contain any set of characters.
Use the below snippet to list files starting with sample
Snippet
There are five files starting with the text sample. All these files will be printed.
Output
You can use any regex pattern along with glob to identify files with a specific pattern.
List Files In Directory Sorted By Name
In this section, you’ll learn how to list all files in a directory and display them in a sorted way.
Using this, you’ll list the files in the directory. The files in the subdirectories will be ignored.
Next, Using list comprehension,
Use the below snippet to list the files in a sorted way.
Snippet
You’ll see the file names listed in an alphabetically sorted way.
Output
You’ve displayed files in the directory sorted by name.
List Files In Directory and Subdirectories Sorted By Name
In this section, you’ll learn how to list files in directory and subdirectories sorted by their name.
To display the files in a directory and its subdirectory, Use
Snippet
You’ll see the output displayed below. First, the files in the directory will be displayed and then the files in the main directory will be displayed in a sorted way.
Output
This is how you can list files sorted by using its name.
Conclusion
To summarize, you’ve learned the different methods available in python to list files in a directory. You’ve also learned how those methods can be used in various scenarios to display the file names is available in the directory.
If you want to list files from directories from the system shell read How to execute a shell command from Python tutorial.