用于查找包含文本字符串的文件的Python脚本
用于查找包含文本字符串的文件的Python脚本 我想找到一个包含特定字符串的文本文件 下面的找不到匹配的文件名和匹配的行号用于查找包含文本字符串的文件的Python脚本,python,Python,用于查找包含文本字符串的文件的Python脚本 我想找到一个包含特定字符串的文本文件 下面的找不到匹配的文件名和匹配的行号 import os search_path = "C:\\Users\\xx\\Desktop\\text_file\\" #folder to search file_type = ".txt" #filetype to search search_str = " INTERIM BILL-SUMMARY" #string to search if not (sear
import os
search_path = "C:\\Users\\xx\\Desktop\\text_file\\" #folder to search
file_type = ".txt" #filetype to search
search_str = " INTERIM BILL-SUMMARY" #string to search
if not (search_path.endswith("/") or search_path.endswith("\\") ): # Append a directory separator if not already present
search_path = search_path + "/"
if not os.path.exists(search_path): # If path does not exist, set search path to current directory
search_path ="."
for fname in os.listdir(path=search_path): # Repeat for each file in the directory
if fname.endswith(file_type): # Apply file type filter
fo = open(search_path + fname) # Open file for reading
line = fo.readline() # Read the first line from the file
line_no = 1 # Initialize counter for line number
while line != '' : # Loop until EOF
index = line.find(search_str) # Search for string in line
if ( index != -1) :
print(fname, "[", line_no, ",", index, "] ", line, sep="")
line = fo.readline() # Read next line
line_no += 1 # Increment line counter
fo.close() # Close the files
你能试试这个,告诉我它对你有用吗
导入操作系统
搜索要搜索的文件夹
file_type=“.txt”#要搜索的文件类型
search_str=“临时账单摘要”#要搜索的字符串
如果不存在(search\u path.endswith(“/”)或search\u path.endswith(“\\”):\35;如果不存在,请附加目录分隔符
搜索路径=搜索路径+“/”
如果不存在os.path.exists(搜索路径):#如果路径不存在,请将搜索路径设置为当前目录
搜索路径=”
对于os.listdir(path=search_path)中的fname:#对目录中的每个文件重复此操作
如果fname.endswith(文件类型):#应用文件类型筛选器
将open(search_path+fname)作为f:#打开文件进行读取
对于第_行,枚举(f)中的第行:
如果搜索行中的字符串:
打印(fname,“[”,行号,“,”,行。查找(搜索字符串),“]”,行,sep=“”)
您能试试这个吗,并告诉我它是否适合您
导入操作系统
搜索要搜索的文件夹
file_type=“.txt”#要搜索的文件类型
search_str=“临时账单摘要”#要搜索的字符串
如果不存在(search\u path.endswith(“/”)或search\u path.endswith(“\\”):\35;如果不存在,请附加目录分隔符
搜索路径=搜索路径+“/”
如果不存在os.path.exists(搜索路径):#如果路径不存在,请将搜索路径设置为当前目录
搜索路径=”
对于os.listdir(path=search_path)中的fname:#对目录中的每个文件重复此操作
如果fname.endswith(文件类型):#应用文件类型筛选器
将open(search_path+fname)作为f:#打开文件进行读取
对于第_行,枚举(f)中的第行:
如果搜索行中的字符串:
打印(fname,“[”,行号,“,”,行。查找(搜索字符串),“]”,行,sep=“”)
我只是偶然发现了这篇文章:)并修改了一些行,以便更好地处理和csv输出
import os
import csv
search_path = input("Enter directory path to search : ")
file_type = input("File Type : ")
search_str = input("Enter the search string : ")
if not (search_path.endswith("/") or search_path.endswith("\\") ): # Append a directory separator if not already present
search_path = search_path + "/"
if not os.path.exists(search_path): # If path does not exist, set search path to current directory
search_path ="."
for fname in os.listdir(path=search_path): # Repeat for each file in the directory
if fname.endswith(file_type): # Apply file type filter
with open(search_path + fname,encoding="utf-8") as f: # Open file for reading
with open('output.csv', 'a+') as output:
#csvwriter = csv.writer(csvfile)
for line_no, line in enumerate(f):
if search_str in line:
output.write("".join(map(str, [line])))
print(fname, "[", line_no, ",", line.find(search_str), "] ", line, sep="")
我只是偶然发现了这篇文章:)并修改了一些行以更好地处理和csv输出
import os
import csv
search_path = input("Enter directory path to search : ")
file_type = input("File Type : ")
search_str = input("Enter the search string : ")
if not (search_path.endswith("/") or search_path.endswith("\\") ): # Append a directory separator if not already present
search_path = search_path + "/"
if not os.path.exists(search_path): # If path does not exist, set search path to current directory
search_path ="."
for fname in os.listdir(path=search_path): # Repeat for each file in the directory
if fname.endswith(file_type): # Apply file type filter
with open(search_path + fname,encoding="utf-8") as f: # Open file for reading
with open('output.csv', 'a+') as output:
#csvwriter = csv.writer(csvfile)
for line_no, line in enumerate(f):
if search_str in line:
output.write("".join(map(str, [line])))
print(fname, "[", line_no, ",", line.find(search_str), "] ", line, sep="")
顺便说一句,我看不出你的原稿有什么问题,我只是重写了一小部分,并在本地检查了一些虚拟文件,似乎为meBTW工作,我看不出你的原稿有什么问题,我只是重写了一小部分,并在本地检查了一些虚拟文件,似乎为我工作你的
搜索str
是临时账单摘要“
,是否可以删除字符串开头的多余空间,然后重试?另外,请确保您的文件内容都是大写的。您的搜索\u str
是“临时账单摘要”
,请删除字符串开头的多余空间,然后重试?此外,请确保文件内容全部大写。