This program lists all directories, subdirectories and files under: “/home/michael/PycharmProjects/CBenson” It writes the list to directory_list.txt. Each line is a separate file or directory.
#!/usr/bin/env python import os path = “/home/michael/PycharmProjects/CBenson” filelist = []
for root, dirs, files in os.walk(path): for file in files: #append the file name to the list filelist.append(os.path.join(root,file))
#Writing a file that has a list of files for the directories #filename that will be appended to filename=”directory_list.txt” myfile=open(filename, ‘a’) #print all the file names for name in filelist: myfile.write(name) myfile.write(‘\n’) myfile.close()