Search directory for specific string

后端 未结 2 2024
攒了一身酷
攒了一身酷 2021-02-19 11:47

I\'m trying to search through a specific directory full of header files, and look through each header file, and if any file has a string \"struct\" in it, I just want the progra

2条回答
  •  一整个雨季
    2021-02-19 11:57

    This works when I test it on my end:

    for files in glob.glob( "*.h" ):
        f = open( files, 'r' )
        file_contents = f.read()
        if "struct" in file_contents:
                print f.name
        f.close()
    

    Make sure you print f.name, otherwise you're printing the file object, and not the name of the file itself.

提交回复
热议问题