Mick,
Are you trying to crawl through a folder looking for files and their path? I use the following to reclusively look for a particular file type in a folder and subfolders. The function call below returns a list containing the folder and filename for all "txt" files in a directory and its subdirectors.
import os, fnmatch def locate(pattern, root=os.curdir): for folder, subs, files in os.walk(root): for filename in fnmatch.filter(files, pattern): yield os.path.join(folder, filename) filelist = locate('*.txt', r'D:\path')
Per your question on how to run this on multiple file extensions, the simplest solution is just to call the locate function multiple times. Such as:
for ext in ('*.txt', '*.thn', '*.csv'): # This will run three times first returning all # the TXT files, then THN,and finally CSV files filelist = locate(ext, r'D:\path') for fp in filelist: # Do what you need to with this file # You can also use the variable 'ext' here to # process files differently base upon the 'ext'
Yes, you are right. I have tried and received "line 9, in <module>with open(filelist,"r") as f:TypeError: coercing to Unicode: need string or buffer, generator found".
wow you were ready for that one!
import os my_folder = r'D:\Downloads' file_list = [] #walk your folder for root, dirs, files in os.walk(my_folder): for filename in files: #join filename to folder path filepath = os.path.join(my_folder, filename) #print to test the functionality print(filepath) #you can also append all the filepaths to a list, rather #than within this generator. #also added a test for .txt in filename if '.txt' in filename: file_list.append(filepath) break
What do you want to actually do?
No fair David, you can type faster than I can.
One other note. Make sure you use a Python raw string for your path. This will treat the backslash (\) as a literal character and not as an escape character. David has included this in is example by using the lower case (r) in front of his path "myfolder" string.
myfolder = r'D:\Downloads'
Unfortunately, I have more than one hundred files in the folder, so it is very difficult to indicate all of them.
Python File Write (w3schools.com) - be very careful which mode you open the textfiles in, as this can delete them inadvertently. e.g. 'w', 'a', 'ab'... I'd really recommend on reading up on them first.
#the os.path.join method is useful for joining paths import os #folder that texfiles will be saved in myfolder = r'D:\Downloads' #name of your textfiles textfile_name_1 = 'demo1.txt' textfile_name_2 = 'demo2.txt' #concatenate(add together) path strings textfile_path_1 = myfolder + '\\' + textfile_name_1 textfile_path_2 = myfolder + '\\' + textfile_name_2 #see the resulting paths print(textfile_path_1) print(textfile_path_2) #or I prefer to use os.path.join textfile_path_1 = os.path.join(myfolder, textfile_name_1) textfile_path_2 = os.path.join(myfolder, textfile_name_2) #see the resulting paths print(textfile_path_1) print(textfile_path_2) #open/create a textfile and write to it #the 'w' means write, but will also delete and #recreate the entire textfile if already exists f = open(textfile_path_1, 'w') f.write('I have created a textfile and written this line to it') f.close() #the with statement means no .close() method is needed with open(textfile_path_2, 'w') as text_file: text_file.write('I have created a tetxfile and written this line to it')
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.