python listing directory,python listing,listing directory,python directory,python methods, Python Listing Directory in  2 effective methods, Shopinson, Shopinson

There are two modified ways or methods of listing the files contained in a specified python listing directory also see PYTHON listing files contained in a directory which explains the detailed note in advance of using os.walker and glob with specified file type but here I described how to simply list files contained in a directory in two simple methods.

python listing directory,python listing,listing directory,python directory,python methods, Python Listing Directory in  2 effective methods, Shopinson, Shopinson
Listing Directory Using Python

1st Method : python listing directory

Legacy method of Directory listing with python

import os
entries = os.listdir('shopinson/')
for entry in entries:
    print(entry)

the above python code snippets displays all files contained in the specified directory named “shopinson

2nd Method : listing directory using python

Modern method of Directory listing with python

#1 Example: 2nd Method Using os.scandir()

import os
with os.scandir('shopinson/') as entries:
    for entry in entries:
        print(entry.name)

#2 Example: 2nd Method Using pathlib.Path()

from pathlib import Path

entries = Path('shopinson/')
for entry in entries.iterdir():
    print(entry.name)

Difference between the two method is that the 1st method uses os.listdir() while the second method uses os.scandir() and pathlib.Path().

Note: to get list of file types including it types best method is the modern method

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