How can I list all files of a directory in python and add them to a list?
Join them; it only takes a minute:
|
|
If you want just files, you could either filter this down using
or you could use
And lastly, as that example shows, adding one list to another you can either use
Personally, I prefer |
|||||||||||||||||||||
|
|
I prefer using the
Will return a list with the queried files:
|
|||||||||||||||||||||
|
will return a list of all files and directories in "somedirectory". |
|||||||||
|
|
A one-line solution to get only list of files (no subdirectories):
or absolute pathnames:
|
|||||||||||||
|
|
Getting Full File Paths From a Directory and All Its Subdirectories
If you'd like, you can open and read the contents, or focus only on files with the extension ".dat" like in the code below:
|
||||
|
|
|
I really liked adamk's answer, suggesting that you use But as other people pointed out in the comments, As examples:
The above is terrible - the path has been hardcoded and will only ever work on Windows between the drive name and the
The above works better, but it relies on the folder name
This works perfectly across all platforms. Another great example that works perfectly across platforms and does something a bit different:
Hope these examples help you see the power of a few of the functions you can find in the standard Python library modules. |
|||||||||
|
|
|||||
|
|
pathlib: New in version 3.4.
os.scandir(): New in version 3.5.
|
|||||||||||||||||
|
|
You should use
|
|||||||||||||
|
os.listdir returns a list containing the names of the entries in the directory given by path. |
|||
|
|
|
If you are looking for python implementation of find, this is a recipe I use rather frequently:
so I made a PyPI package out of it and there is also a github repository. I hope that someone finds it potentially useful for his code. |
||||
|
|
|
Returning a list of absolute filepaths, does not recurse into subdirectories
|
|||||
|
|
Python 3.5 introduced new, faster method for walking through the directory - Example:
|
|||
|
|
|
List all files in a directory:
Here, you get list of all files in a directory. |
||||
|
|
|
||||
|
|
|
Python 3: for the list of files in the current work directory
to include them in a list, just:
If you want to have the list of a particular directory, not the directory where the terminal (or cmd) is:
Python 2 works differently...
The code above will print all the files in the current directory The same here:
To go up in the directory tree, you got to code like this:
and this...
... this will print all the files in the root directory
|
|||||||||||||||||||||
|
|
If you care about performance, try
This save a lot of time when you need to scan a huge directory, you do not need to buffer a huge list, just fetch one by one. And also you can do it recursively:
|
|||
|
|
|
|||||
|
|
Use this function if you want to different file type or get full directory.
|
|||
|
|
|
By using
|
|||
|
|
|
Using generators
|
|||
|
|
protected by matt Dec 18 '14 at 2:54
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?