Home > database >  Print matching String, of list of Strings, for each filename
Print matching String, of list of Strings, for each filename

Time:06-28

I am using Os.Walk to get all filenames, now I want to see if each filenames has certain string in them.

If they do I would like to print does matching words.

I am aware that my current code won't do that.
This is part of a larger code, which should give a dic, with bunches of info about the files in the end. But I can't write down the filenames, so I need to see if these words exist, through my code.

What I have:

import os

match = ["draft", "first", "test"]

for filenames in os.walk("C:\\Users\"):
    if filenames is like match:
        print(#what_matches)

CodePudding user response:

Try modifying this code might work for you:

for dirpaths, dirnames, filenames in os.walk('/Users/meow/meow'):
    print(f"There\'re {len(dirnames)} directories and {len(filenames)} files in {dirpaths}")

CodePudding user response:

If you want to check if the strings in the list are inside the file, then you need first read the files.

  • Related