打开具有更改的特定文件模式的文件名

问题描述

我使用下面的代码文件中读取内容,因为我需要将 .mnu 文件内容复制到另一个 .fld 文件中。

with open(MS577007.mnu') as myfile:
        contents = myfile.read()
print(contents)

.mnu 文件每个月都会更改,因此我正在编写一个脚本来自动执行此操作。如何使用正则表达式打开 .mnu 文件中的内容? *.mnu 不起作用。

解决方法

您需要获取所有以(扩展名为)mnu 结尾的文件名。

from glob import glob

# Find all files
files = glob("*.mnu")
# Loop in files as file
for file in files:
    # Open each file and print content
    with open(file,"r") as myfile:
        contents = myfile.read()
        print(contents)