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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.