Python exercise - list all file under a path

Below is an sample for listing all files under a ftp path.
At first I think of using for loop to list folder, but I had to decide how much loop to get into.
After reading an article on mystackoverflow, someone throw an idea to use recursion and it helps shrinking code into a couple line! <3

file = open("log.txt",'w')

def recurlist(path):
for fileattr in CME_sftp.listdir_attr(path):
    if stat.S_ISDIR(fileattr.st_mode):        #Loop if it is a path, print the filename instead
    print path + "/" + fileattr.filename
    try:                                                     #This prevent from authentication access
    recurlist(path + "/" + fileattr.filename)
    except:                                               #I dont know if I have to do this but just in case
    pass
    else:
    file.write(path + "/" + fileattr.filename + '\n')

recurlist(start)

Comments