Excellent PYTHON listing files contained in 1 directory

How to list all files of a directory using Python

0
853
python lists files contained in a directory_
python lists files contained in a directory_

Python Can list the files contained in a defined directory which may include the sub-directory.

Today in our programming Hub tutorial will we be writing a simple Python script which displays the lists of files contained in a directory (defined) or its sub-directory.

This simple script of displaying files in a defined directory is categorized in two

  1. Os.walker
  2. Glob

1. Python – To list the files in a directory using os.walker

1.1 : Python Displaying files in a directory based on the file extension type specified

For example our below code displays ONLY filenames with ā€œ.phpā€ file extension according as contained in the directory excluding the files which its extension are not specified.

import os

path = 'c:\\pythonwork\\'

files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if '.php' in file:
            files.append(os.path.join(r, file))

for f in files:
    print(f)

Note: this simple Python script for displaying filenames contained in a defined directory based on file type extension does not only display the files in that particular directory but includes those contained in its sub-directory.

Output 1.1 :

python-listing-files-in-directory-by-extension
python-listing-files-in-directory-by-extension

Notice only files with php file extension are displayed

1.2 Python – Displays all files in a directory

This code snippet below displays ALL files contained in a directory excluding none

import os

path = 'c:\\pythonwork\\'

files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
            files.append(os.path.join(r, file))

for f in files:
    print(f)

This code snippet above displays all files contained in the directory including those in its sub-directory

Output  1.2

python-listing-all-files-in-directory
python-listing-all-files-in-directory

#changing the file extension to make Python find and display files with the specified extension type

python-listing-files-in-directory-by-extension-changing
python-listing-files-in-directory-by-extension-changing

From the image above when the .php extension when changed to .txt, .doc, .js, .css the python only displays the files with the specified extension.

2. Python – To list the files in a directory using glob

2.1: Python ā€“ displays files based on file extension

Behaves Same as the 1.1 snippet but the below using glob module in Python

import glob

path = 'c:\\pythonwork\\'

files = [f for f in glob.glob(path + "**/*.php", recursive=True)]

for f in files:
    print(f)

This displays ONLY file based on the file extension present in the defined directory path excluding files that are not specified

Output: (as shown in Output 1.1) above

2.2: Python ā€“ display files using glob

import glob

path = 'c:\\pythonwork\\'

files = [f for f in glob.glob(path + "**/*", recursive=True)]

for f in files:
    print(f)

This displays ALL file present in the defined directory path not excluding any files.

Output: (as shown in Output 1.2) above

LEAVE A REPLY

Please enter your comment!
Please enter your name here