Solved! Go to Solution.
import os prompt = "Please enter the directory path: " dirPath = raw_input(prompt) listing = os.listdir(dirPath) fileName = r"C:\FileList.txt" fileList = open(fileName, "w+") fileList.write(dirPath + "\n\n") fileList.write("DIRECTORIES:\n") for dir in listing: if os.path.isdir(dir): fileList.write(dir + "\n") else: fileList.write("Not a directory: " + dir + "\n") #s += "\n".join(dir) #fileList.write(s + "\n") listing = os.listdir(dirPath) fileList.write("\n") fileList.write("FILES:\n") for file in listing: if os.path.isfile(file): fileList.write(file + "\n") else: fileList.write("Not a file: " + file + "\n") fileList.close() prompt = "File list created at: " + fileName + "\nPress Enter" ok = raw_input(prompt)
C:\Users\gkeith\Desktop\PythonScripts DIRECTORIES: Not a directory: abc00000.txt Not a directory: abc0000x.txt Not a directory: abc000xx.txt Not a directory: abc00zzz.txt Not a directory: abc0xxxx.txt Not a directory: abcuvwxyz.txt Not a directory: abcxxxxx.txt Not a directory: FileLister2.py Not a directory: New folder Not a directory: New folder (2) Not a directory: New folder (3) Not a directory: RenamePDF.py FILES: Not a file: abc00000.txt Not a file: abc0000x.txt Not a file: abc000xx.txt Not a file: abc00zzz.txt Not a file: abc0xxxx.txt Not a file: abcuvwxyz.txt Not a file: abcxxxxx.txt FileLister2.py Not a file: New folder Not a file: New folder (2) Not a file: New folder (3) Not a file: RenamePDF.py
import os testdir = "C:\\test" for dir in os.listdir(testdir): dir = os.path.join(testdir,dir) if os.path.isdir(dir): print dir + " is a directory" elif os.path.isfile(dir): print dir + " is a file" else: print dir + " is not a directory"