Hi,
Below is a small script I am working on. It lists all the files in a directory.
I am unsure how to get the print(elem) to save the data to a .txt file.
Any help would be great.
_
Below is a small script I am working on. It lists all the files in a directory.
I am unsure how to get the print(elem) to save the data to a .txt file.
Any help would be great.
_
import os
'''
For the given path, get the List of all files in the directory tree
'''
name = input('Please Input The Directory')
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
return allFiles
def main():
dirName = name;
# Get the list of all files in directory tree at given path
listOfFiles = getListOfFiles(dirName)
# Print the files
#for elem in listOfFiles:
# print(elem)
#print ("****************")
# Get the list of all files in directory tree at given path
listOfFiles = list()
for (dirpath, dirnames, filenames) in os.walk(dirName):
listOfFiles += [os.path.join(file) for file in filenames]
# Print the files
for elem in listOfFiles:
print(elem)
outF = open("myOutFile.txt", "w")
outF.writelines("main")
outF.close()
#import subprocess
#subprocess.Popen(r'"C:\windows\system32\notepad.exe" "myOutFile.txt"')
if __name__ == '__main__':
main()
