Python Regex替换扩展名';从右边第一次出现

Python Regex替换扩展名';从右边第一次出现,python,Python,假设我有以下文件 In [2]: os.listdir() Out[2]: ['brmdh.md', '.DS_Store', 'breakfast.md', 'test.sh', 'bmdt.md'] 我想将扩展名从.md更改为.py,其他扩展名保持不变 请注意“brmdh.md”、“bmdt.md” 我试

假设我有以下文件

In [2]: os.listdir()                                                                                              
Out[2]: ['brmdh.md', '.DS_Store', 'breakfast.md', 'test.sh', 'bmdt.md']
我想将扩展名从.md更改为.py,其他扩展名保持不变

请注意“brmdh.md”、“bmdt.md”

我试着处理其中一个

re.sub("a patter specify .md at the end of string", "py", 'brmdh.md')
我无法完成它,并发现它方便,如果有一个子从右侧取代“md”的第一次出现

re.rsub(r"md{1}", "py", 'brmdh.md')
是否可以使用lib-re使其工作?

re.sub(r'(?P<file_name>.*)\.md$', '\g<file_name>.py', 'hello.md')  # hello.py
re.sub(r'(?P.*)\.md$,'\g.py',hello.md')\hello.py