How to List Files and Folders With Python

Are you wondering How to List Files and Folders with Python? You can Easily List Files and Folders with Python Code from any Path of your system. The Default included module ‘OS’ in Python will be greatly helpful for performing operating system-level functions.

OS module has many file related functions like listdir() –  for listing the contents of any Path or Dir ,which we use in the script for count and view files and folders at any path of system.


Here is a small code for count and Display List of Files and Folders in Python (Python 3):

import os
path = raw_input("Enter the Path: ")
lst = os.listdir(path)
total = len(lst)
print "No. of Total files and folders at ", path ," = ", total
i = raw_input("nType v and press Enter for view list nPress any other key to exit.... n")
if i=='v':
    for f in lst:
        print f


There are also many other powerful functions like chmod() – used for changing permissions of files.