Op Wednesday 20 May 2015 01:20 CEST schreef MRAB: > On 2015-05-19 23:23, Cecil Westerhof wrote: >> Op Tuesday 19 May 2015 23:28 CEST schreef Jon Ribbens: >> >>> On 2015-05-19, Cecil Westerhof <ce...@decebal.nl> wrote: >>>> It looks like that this does what I want (the dot is needed so >>>> that it also works with 2.7): files = sorted(os.listdir('.')) p = >>>> re.compile('actions-2015-05-[0-9][0-9].sql$') current_month = [ >>>> file for file in files if p.match(file) ] >>> >>> You could instead do (in Python 2 or 3): >>> >>> files = glob.glob("actions-2015-05-[0-9][0-9].sql") >>> files.sort() >> >> Something to remember. >> >> But in this case I also need the previous month. So I have: >> files = sorted(os.listdir('.')) >> p = re.compile('actions-2015-05-[0-9][0-9].sql$') >> current_month = [ file for file in files if p.match(file) ] >> p = re.compile('actions-2015-04-[0-9][0-9].sql$') >> previous_month = [ file for file in files if p.match(file) ] >> >> Of-course I will not hard-code the months in the real code. >> > In a regex, '.' will match any character except '\n', or any > character at all if the DOTALL ('(?s)') flag in turned on. If you > want to match an actual '.', you should escape it like this: r'\.'. > (And if you're using backslashes in a string literal, make it a raw > string literal!) > > p = re.compile(r'actions-2015-05-[0-9][0-9]\.sql$')
Oops, you are correct. It is now: start = database + '-' end = r'-[0-9][0-9]\.sql$' p = re.compile(start + current_month + end) current_month_lst = [ file for file in files if p.match(file) ] p = re.compile(start + previous_month + end) previous_month_lst = [ file for file in files if p.match(file) ] -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list