1

I have the following method in my python email code

def main():
    names, emails = get_contacts('mycontacts.txt') # read contacts
    message_template = read_template('message.txt')

I'd like to be able to have it read all files that end in .txt into the message_template.

Something like:

message_template = read_template('*.txt')

Thanks in advance

Sean Breckenridge
  • 1,932
  • 16
  • 26
rahrahruby
  • 673
  • 4
  • 11
  • 28
  • There's the re module which you could use to match on a wildcard or the glob module. re is python's builtin regex module while glob does unix style pathname pattern expansion. – R. Arctor Aug 26 '19 at 22:38
  • you can't read templates with `*` . You can only use `glob.glob("*.txt")` to get all filenames in folder and later use loop to read all templates separatelly and eventually concatenate them into one string. – furas Aug 26 '19 at 22:40
  • Awesome thanks, I will try the glob option – rahrahruby Aug 26 '19 at 22:40
  • btw: I'm not sure if it will give filenames always in the same order so it is good to use numbers in filenames `1-message.txt`, `2-footer.txt` and sort filenames before you start reading and concatenating files. – furas Aug 26 '19 at 22:42
  • Excellent point, thanks – rahrahruby Aug 26 '19 at 22:43
  • The filenames wont be in any specific order, `glob.glob` depends on `os.listdir`, whose order is [arbitrary](https://docs.python.org/3/library/os.html#os.listdir) – Sean Breckenridge Aug 26 '19 at 23:12

0 Answers0