Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

Read all files in the Directory python

Python Program to read the contents of all the files located in the directory

Program or Solution

					    
							
from os import listdir
from os.path import isfile, join
files = [f for f in listdir("/home/administrator/files") if isfile(join("/home/administrator/files", f))]
for file in files:
    print(file)
    f=open(file,'r')
    print(f.read())
    print("\n\n End of File \n\n")
    
    


					    
					

Program Explanation

Collect the files in the directory using listdir() method.

Using "for" open each file in read mode and read the content of file and close the file.