Home » Python

List all files of a directory in Python

Listing all files of a directory: Here, we are going to learn how to list all files of a directory in Python programming language using os.walker and glob?
Submitted by Sapna Deraje Radhakrishna, on October 22, 2019

Python provides built-in modules like os.walker or glob to build a find like function to list files or folders in given directories and its subdirectories.

os.walker

1) List all files with an extension as .py in a provided directory and sub directory

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> cwd =os.getcwd()
>>> print("current dir {}".format(cwd))
current dir /home/user/Desktop/my_work/python_samples
>>> files = []
# r=root, d=directory, f=files
>>> for r, d, f in os.walk(cwd):
...     for file in f:
...             if '.py' in file:
...                     files.append(os.path.join(r, file))
...
>>> for f in files:
...     print(f)
...

Output

/home/user/Desktop/my_work/python_samples/src/samples/regular_expression_examples.py
/home/user/Desktop/my_work/python_samples/src/samples/if_main.py
/home/user/Desktop/my_work/python_samples/src/samples/second_module.py
/home/user/Desktop/my_work/python_samples/src/samples/sub_process_samples.py
/home/user/Desktop/my_work/python_samples/src/samples/dictionaries.py

2) List all directories and sub directories

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> cwd =os.getcwd()
>>> print("current dir {}".format(cwd))
current dir /home/user/Desktop/my_work/python_samples
>>> folders = []
>>> for r,d,f in os.walk(cwd):
...     for folder in d:
...             folders.append(os.path.join(r, folder))
...
>>> for f in folders:
...     print(f)

Output

/home/user/Desktop/my_work/python_samples/src
/home/user/Desktop/my_work/python_samples/test
/home/user/Desktop/my_work/python_samples/test_1

glob

1) List all files with an extension as .py in a provided directory and sub directory

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

import glob
cwd = "/home/user/Desktop/my_work/python_samples/"
>>> files = [f for f in glob.glob(cwd + "**/*.py", recursive=True)]
>>> for f in files:
...     print(f)

['/home/user/Desktop/my_work/python_samples/src/samples/regular_expression_examples.py', '/home/user/Desktop/my_work/python_samples/src/samples/if_main.py', '/home/user/Desktop/my_work/python_samples/src/samples/second_module.py', '/home/user/Desktop/my_work/python_samples/src/samples/sub_process_samples.py']

2) List all directories and sub directories

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

import glob
cwd = "/home/user/Desktop/my_work/python_samples/"
>>> folders = [f for f in glob.glob(cwd + "**/", recursive=True)]
>>> for f in folders:
...     print(f)

Output

/home/user/Desktop/my_work/python_samples/
/home/user/Desktop/my_work/python_samples/src/
/home/user/Desktop/my_work/python_samples/src/samples/


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.