codescracker


python

Python Program to List all Files in Directory



« Previous Program Next Program »


List all Files in Directory in Python

To list all the files present in the directory given by the user in python, first import os and glob library and then use os.chdir() to get the desired directory and then use glob.glob() to find all the files present inside that directory as shown in the program given below:

Python Programming Code to List all Files in Directory

Following python program list all the files present in the C:/Python34 directory with .txt extension:

# Python Program - List Files in Directory
		
import glob, os
os.chdir("C:/Python34")
for file in glob.glob("*.txt"):
	print(file)

Here is the sample run of the above python program to illustrate how to list and print all the files present in any directory:

python list files in directory

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test