Open file by filename wildcard

后端 未结 5 764
耶瑟儿~
耶瑟儿~ 2020-12-13 14:29

I have a directory of text files that all have the extension .txt. My goal is to print the contents of the text file. I wish to be able use the wildcard *

5条回答
  •  萌比男神i
    2020-12-13 14:57

    This problem just came up for me and I was able to fix it with pure python:

    Link to the python docs is found here: 10.8. fnmatch — Unix filename pattern matching

    Quote: "This example will print all file names in the current directory with the extension .txt:"

    import fnmatch
    import os
    
    for file in os.listdir('.'):
        if fnmatch.fnmatch(file, '*.txt'):
            print(file)
    

提交回复
热议问题