Python,files contained,directory,list files,directory file listing, Excellent PYTHON listing files contained in 1 directory, Shopinson, Shopinson

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,files contained,directory,list files,directory file listing, Excellent PYTHON listing files contained in 1 directory, Shopinson, Shopinson
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,files contained,directory,list files,directory file listing, Excellent PYTHON listing files contained in 1 directory, Shopinson, Shopinson
python-listing-all-files-in-directory

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

Python,files contained,directory,list files,directory file listing, Excellent PYTHON listing files contained in 1 directory, Shopinson, Shopinson
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

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

shopinson-logo-32

©2020 Shopinson: complete guide to programming language

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account