Reading and writing

2776
12
Jump to solution
03-06-2021 12:18 AM
Mick
by
Emerging Contributor
 
0 Kudos
12 Replies
LanceCole
MVP Regular Contributor

Sorry, I left the "with" out of the statement when I cut and pasted.  I have corrected my previous post.

0 Kudos
LanceCole
MVP Regular Contributor

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'

 

LanceCole
MVP Regular Contributor

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'
0 Kudos