0

I have like 100+ file in directory and I need to find out which files contain the word in Thai that I looking for

Thank you

I try this but it doesn't work `

import pandas as pd
import re
import os

FOLDER_PATH = r'C:\Users\project'

list = os.listdir(FOLDER_PATH)

 def is_name_in_csv(word,csv_file):
  with open(csv_file,"r") as f:
    data = f.read()
  return bool(re.search(word, data))

word = "บัญชีรายรับ"
for csv_file in list:
  if is_name_in_csv(word,csv_file):
    print(f"find the {word} in {csv_file}")

`

1 Answers1

0

You don't need regex. You can simply check if word in fileContents. Also, I changed list to paths because list is a built-in python keyword.

import os

paths = os.listdir(r'C:\Users\project')

def files_with_word(word:str, paths:list) -> str:
    for path in paths:
        with open(path, "r") as f:
            if word in f.read():
                yield path

#if you need to do something after each found file
for filepath in files_with_word("บัญชีรายรับ", paths):
    print(filepath)

#or if you just need a list
filepaths = [fp for fp in files_with_word("บัญชีรายรับ", paths)]

OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
  • at first it work properly but today it's keep show error "[WinError 3] The system cannot find the path specified" but I doesn't change any path how can I fix this – Tanatchat Laemkom Film Nov 21 '22 at 15:28
  • That's a completely new problem, and needs a new question with the proper details and code. Make sure the paths you are passing to the loop exist on your file system, cause windows is saying they don't. – OneMadGypsy Nov 21 '22 at 16:54