The following code will be used to write a recursive function and display files
<u>Explanation:</u>
#Import required packages
import os
#Define recursive function
#displayFiles() has a single argument
#which may be either pathname or filename
def displayFiles(pathname):
#if the given argument is pathname for directory
if (os.path.isdir(pathname)):
#open the directory
for item in os.listdir(pathname):
#append items in the list
newItem = os.path.join(pathname, item)
#print each filename
#call the function recursively
displayFiles(newItem)
#otherwise if the pathname
#is filename
else:
#set the pathname to filename
filename=pathname
baseFile = os.path.basename(filename)
print("File Name: ", baseFile)
#open the file in read mode
with open(filename, "r") as file:
print("Content:")
#display the contents of the file
for line in file:
#print line
print(line)
print()