Python 3 simple ways to list files in a folder

In this post you can see how to list and filter files in a folder by using Python 3.7. Three of them are simple but efficient and work on Windows, Linux and MacOS. Which one you will chose depends on your packages and needs:

  • os.scandir() - since Python 3.5. It is advertised as PEP 471 -- os.scandir() function -- a better and faster directory iterator
  • os.listdir() - compatible with python 2.7 which make it good if you need to list files in both version Python 2 and 3
  • os.walk() - method for recursive iteration of files and folders in a given directory

You can see video tutorial on this page: Python 3 simple ways to list files and folders

os.scandir() - since Python 3.5

If you are using Python 3.7 the best way to list all files in your project will by: os.scandir(). It allows many different options like:

  • no parameters - list current folder
  • parameter - list the given folder

Note that os.scandir() is not recursive and if you want to list nested folders you can see os.walk() which is recursive by default.

import os
my_files = [f.name for f in os.scandir() if f.is_file()]
print(my_files)

result:

['Read.py', '_my_file.txt', 'listFiles.py', 'Write.py', 'Other.py', 'moveFolder.py', '__init__.py']

If you want to list files in a given folder than you can use:

import os
my_files = [f.name for f in os.scandir('/tmp/test') if f.is_file()]
print(my_files)

result:

['20180925.bak', '20180926.bak', '20180927.txt', '20180926.txt', '20180925.txt']

The last example shows you how to list only files which ends with a given pattern - .bak :

import os
my_files = [f.name for f in os.scandir('/tmp/test') if f.name.endswith('.bak')]
print(my_files)

result:

['20180925.bak', '20180926.bak']

os.listdir() - compatible with python 2.7

The best advantage for me of using os.listdir() for showing all files and folders in a given directory is compatibility with python 2. Python 2 is still very popular and the usage of it is still big. If you want to list folders in the current folder than you can do:

import os
my_files = os.listdir()
print(my_files)

result:

['Read.py', '_my_file.txt', 'listFiles.py', 'Write.py', 'Other.py', 'moveFolder.py', '__init__.py']

Again the second example shows how to list and print all files of a given path:

import os
my_files = os.listdir('/tmp/test')
print(my_files)

result:

['20180925.bak', '20180926.bak', '20180927.txt', '20180926.txt', 'archive', '20180925.txt']

Finally we can show all text files of a folder by using:

import os
my_files = [x for x in os.listdir('/tmp/test') if x.endswith(".bak")]
print(my_files)

result:

['20180927.txt', '20180926.txt', '20180925.txt']

os.walk() - method iterating files in a given folder

The last one is useful when you want to collect additional information like:

  • r=root
  • d=directories
  • f = files
import os

for r, d, f in os.walk('/tmp/test'):
    for file in f:
        if ".bak" in file:
            print(os.path.join(r, file)

The result of this execution is:

/tmp/test/20180925.bak
/tmp/test/20180926.bak
/tmp/test/archive/20180924.bak