os.walk() 匹配 CSV 文件中文件的内容
os.walk() to match contents of a files from a CSV file
I have two words i would like to search for using a CSV file, this is done using the os.walk() method to recursively look through each file within the rootDir
however not too sure on what i'm missing to complete my code. The two words i am looking for is which are in two separate files:
XZOXNEOXXTWX, YOEYTWOYZYNY
首先我创建了一个 csv 文件来查找某些单词,然后我创建了 os.walk()
方法并尝试从 CSV 文件中读取文本以输出匹配的内容。我已经浏览了相当多的 material 但我希望它与我想要输出的内容不匹配。
appendData = []
mPath = r"C:\Users\test\Documents\test"
wordstoSearch = r"C:\Users\test\Documents\test\strings.csv"
for rootDir, subDir, files in os.walk(mPath, topdown=True):
print('Root Directory:', rootDir)
for x in files:
with open(os.path.join(files)):
with open ('strings.csv', 'rt') as stringSearch:
if wordstoSearch in stringSearch.read():
appendData.append('File: {}\nMatching Content: {}\n'.format(x, wordstoSearch))
appendData = '\n'.join(appendData)
print (appendData)
您应该使用脚本顶部的 csv
阅读一次要检查的单词。然后注意如何构建要检查的文件名 - 您需要将根目录加入文件名。最后,读取每个目标文件,然后通过单词列表进行检查。
import csv
appendData = []
mPath = r"C:\Users\test\Documents\test"
wordstoSearch = r"C:\Users\test\Documents\test\strings.csv"
# get the words to check
with open(wordstoSearch) as fp:
reader = csv.reader(fp)
words = [cell.strip() for cell in next(reader)]
# walk the tree
for rootDir, subDir, files in os.walk(mPath, topdown=True):
print('Root Directory:', rootDir)
for x in files:
with open(os.path.join(rootDir, x)) as check_fp:
text = check_fp.read()
for word in words:
if word in text:
appendData.append(
'File: {}\nMatching Content: {}\n'.format(x, word))
appendData = '\n'.join(appendData)
print (appendData)
I have two words i would like to search for using a CSV file, this is done using the os.walk() method to recursively look through each file within the
rootDir
however not too sure on what i'm missing to complete my code. The two words i am looking for is which are in two separate files:
XZOXNEOXXTWX, YOEYTWOYZYNY
首先我创建了一个 csv 文件来查找某些单词,然后我创建了 os.walk()
方法并尝试从 CSV 文件中读取文本以输出匹配的内容。我已经浏览了相当多的 material 但我希望它与我想要输出的内容不匹配。
appendData = []
mPath = r"C:\Users\test\Documents\test"
wordstoSearch = r"C:\Users\test\Documents\test\strings.csv"
for rootDir, subDir, files in os.walk(mPath, topdown=True):
print('Root Directory:', rootDir)
for x in files:
with open(os.path.join(files)):
with open ('strings.csv', 'rt') as stringSearch:
if wordstoSearch in stringSearch.read():
appendData.append('File: {}\nMatching Content: {}\n'.format(x, wordstoSearch))
appendData = '\n'.join(appendData)
print (appendData)
您应该使用脚本顶部的 csv
阅读一次要检查的单词。然后注意如何构建要检查的文件名 - 您需要将根目录加入文件名。最后,读取每个目标文件,然后通过单词列表进行检查。
import csv
appendData = []
mPath = r"C:\Users\test\Documents\test"
wordstoSearch = r"C:\Users\test\Documents\test\strings.csv"
# get the words to check
with open(wordstoSearch) as fp:
reader = csv.reader(fp)
words = [cell.strip() for cell in next(reader)]
# walk the tree
for rootDir, subDir, files in os.walk(mPath, topdown=True):
print('Root Directory:', rootDir)
for x in files:
with open(os.path.join(rootDir, x)) as check_fp:
text = check_fp.read()
for word in words:
if word in text:
appendData.append(
'File: {}\nMatching Content: {}\n'.format(x, word))
appendData = '\n'.join(appendData)
print (appendData)