Requirements: Given a path, to print out all the paths (the path is not until the next directory up)
Import OS path_list = [] DEF get_all (path): # path = r'D: \ the Test3 ' Paths = the os.listdir (path) # lists all the directories and files in the specified directory for I in Paths: com_path = OS. path.join (path, i) # Print (com_path) IF os.path.isdir (com_path): get_all (com_path) # If the path is a directory, calls itself the method elif os.path.isfile (com_path): path_list. the append (com_path) # If the path is a file, appended to the path_list # print (com_path) print each file absolute path print(com_path) # print the absolute path of all files and directories Print ( " D: \ Test3 includes the following files: " ) # call the function get_all (r = path ' D: \ Test3 ' )
Test Results: