Write a recursive function, displayFiles, that expects a pathname as an argument. The path name can be either the name of a file or the name of a directory. If the pathname refers to a file, its filepath is displayed, followed by its contents, like so:

Respuesta :

Answer:

Here is the Python function:

import os   #module used to interact with operating system

def displayFiles(pathname):  #recursive function that takes a pathname as argument

  if (os.path.isdir(pathname)):  #checks if specified path (argument) is an existing directory

      for content in os.listdir(pathname):  #gets the list of all files and directories in the directory and iterates through the items of this list of directory

          contents = os.path.join(pathname, content)  #joins contents of path

          displayFiles(contents)  #calls function recursively

  else:  #if pathname refers to a file

      filename=pathname  #sets filename to pathname

      file = os.path.basename(filename)  #gets base name in specified path

      print("File Name: ", file) #displays the name of file

      with open(filename, "r") as contents:  #opens file in read mode

          print("Content:")  #prints Content:

          for lines in contents:  #iterates through the contents of file

              print(lines)   #displays the contents of file

Explanation:

The recursive function  displayFiles contains a single argument  pathname which can either be a pathname or a filename  . If the given argument is pathname for directory  , then the directory is opened and os.listdir returns a list containing the names of the entries in the directory given by pathname and append these contents in the list. Then this function is called recursively to print each file name and this function is applied to each name in the directory. However if the pathname refers to a file then the pathname is set to filename , the file is opened in read mode and the contents of the file are displayed.

Functions are set of related codes, that are named and grouped in a block, where they perform as a unit code.

The function in Python where comments are used to explain each line is as follows

#This imports the os module

import os

#This defines the function

def displayFiles(pathname):

   #If the path name is a directory

   if (os.path.isdir(pathname)):

       #This iterates through the contents in the directory

       for content in os.listdir(pathname):

           #This joins the contents and calls the function, recursively

           displayFiles(os.path.join(pathname, content))

   #if otherwise that the path name is a filename

   else:  

       #This sets the filename

       fname=pathname

       #This gets the base name in the path

       file = os.path.basename(fname)  

       #This opens and reads the file

       with open(fname, "r") as contents:

           #This iterates and prints each lines

           for lines in contents:

               print(lines)

At the end of the function, the function either displays the files in the directory or the contents of the file

Read more about files at:

https://brainly.com/question/4720685