Read all files in the Directory python

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

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#write your code here

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.

Comments


Related Programs