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

时间:2021-07-27 14:22:00

标签: python file glob

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

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

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

1 个答案:

答案 0 :(得分:1)

您需要获取所有以(扩展名为)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)
相关问题