Regular expression to match start of filename and filename extension

后端 未结 8 836
离开以前
离开以前 2020-12-08 15:35

What is the regular expression to match strings (in this case, file names) that start with \'Run\' and have a filename extension of \'.py\'?

The regular expression s

8条回答
  •  离开以前
    2020-12-08 16:18

    I don't really understand why you're after a regular expression to solve this 'problem'. You're just after a way to find all .py files that start with 'Run'. So this is a simple solution that will work, without resorting to compiling an running a regular expression:

    import os
    for filename in os.listdir(dirname):
        root, ext = os.path.splitext(filename)
        if root.startswith('Run') and ext == '.py':
            print filename
    

提交回复
热议问题