Question: Programming Exercise 6.7 I get the error name 'ranged' is not defined in line 9. I have tried everything I can to get this to

Programming Exercise 6.7 I get the error name 'ranged' is not definedProgramming Exercise 6.7in line 9. I have tried everything I can to get this

I get the error name 'ranged' is not defined in line 9. I have tried everything I can to get this to work.

This is the instructions:

Write a recursive function, displayFiles, that expects a pathname as an argument. The pathname can be either the name of a file or the name of a directory. If the pathname refers to a file, its name is displayed, followed by its contents. Otherwise, if the pathname refers to a directory, the function is applied to each name in the directory. Test this function in a new program.

THis is the code I have written:

def readFile (filename): with open (filename, 'r') as f: for line in f: print (line) def findFile (pathanme, d): indent='' for i in ranged(d): indent = indent +'' if (os.path.isdir(pathname)): for item in os.listdir(pathname): newItem = os.path.join(pathname, item) print(indent+newItem)

if(os.path.isdier(newItem)): findFile(newItem, d + 1)

else: Print(indent + pathname) readFile (pathname) findFile('tests\hello.txt', 0)

viewfiles.py x /root/sandbox viewfiles.py 1# Put your code here 3 def readFile (filenane) 4 with open (filenane, r) as f for line in f print (line) 7 def findFile (pathanne, d) indent-'" 9 for i in ranged (d) 10 indent indent +' 12 if (os.path.isdir (pathnane)) 13 for iten in os.listdir(pathnane) 14 15 16 print(indent+newIten) 17 18 19 if (os.path.isdier (newIten)) 20 findFile (newIten, d 1) 21 22 else 23 24 25 26 findFile(' teststhello.txt', ) 27 newItenos.path.join (pathnane, iten) Print(indent pathnane) readFile (pathnane)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!