【问题标题】:Open a filename with specific file pattern that changes打开具有更改的特定文件模式的文件名
【发布时间】:2021-07-27 14:22:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: python file glob


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 2021-06-30
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 2016-03-20
      相关资源
      最近更新 更多