Best way to rewrite Popen

MRAB python at mrabarnett.plus.com
Tue May 19 19:20:30 EDT 2015


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 <Cecil at 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$')




More information about the Python-list mailing list