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"
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
s += "\n".join(dir)
In the PythonWin interactive window[...]
Yup, definitely some system or environment setting throwing a wrench in things. What does this return?import os os.getenv("PYTHON")
import os os.getenv("PYTHON")
Don't use backslashes in paths. It escapes the next character.
fileName = "C:\FileList.txt"
import os prompt = "Please enter the directory path: " dirPath = raw_input(prompt) listing = os.listdir(dirPath) fileName = "C:\FileList.txt" s = "??" 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") s += dir.join("\n") 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") fileList.close() prompt = "File list created at: " + fileName + "\nPress Enter" ok = raw_input(prompt)
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.