我正在尝试在目录中包含的文件中查找字符串。我有一个像banana
这样的字符串,我知道它存在于一些文件中。
import os
import sys
user_input = input("What is the name of you directory?")
directory = os.listdir(user_input)
searchString = input("What word are you trying to find?")
for fname in directory: # change directory as needed
if searchString in fname:
f = open(fname,'r')
print('found string in file %s') %fname
else:
print('string not found')
当程序运行时,它只为每个文件输出string not found
。有三个文件包含单词banana
,因此程序无法正常工作。为什么不在文件中找到字符串?
答案 0 :(得分:2)
您正试图在A
中搜索string
,使用filename
:
open(filename, 'r').read()
我们使用import os
user_input = input('What is the name of your directory')
directory = os.listdir(user_input)
searchstring = input('What word are you trying to find?')
for fname in directory:
if os.path.isfile(user_input + os.sep + fname):
# Full path
f = open(user_input + os.sep + fname, 'r')
if searchstring in f.read():
print('found string in file %s' % fname)
else:
print('string not found')
f.close()
获取完整路径
user_input + os.sep + fname
提供文件和目录名称,因此我们使用os.listdir
来检查文件。
答案 1 :(得分:0)
这是我在stackoverflow中的第一个问题,请问格式化问题时出现的错误。 我是您知识(编程)领域的完整入门者。 预先谢谢你。
我在木星中尝试过
sudo apt install -y cmake
输出:
您的目录名称是什么?k:\ 0 \ 000.THU.EEG.nedc_tuh_eeg
您想找到什么单词?
它导致:
import os
user_input = input("What is the name of you directory?")
directory = os.listdir(user_input)
searchString = input("What word are you trying to find?")
for fname in directory:
if os.path.isfile(user_input + os.sep + fname):
with open(user_input + os.sep + fname, 'r') as file_handler:
for line in file_handler:
if searchString in line:
print('found string in file %s' %(fname))
break
答案 2 :(得分:0)
这是我能给您的最简单的答案。您不需要颜色,它们只是很酷,您可能会发现您可以在我的代码中学到很多东西:)
import os
from time import sleep
#The colours of the things
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# Ask the user to enter string to search
search_path = input("Enter directory path to search : ")
file_type = input("File Type : ")
search_str = input("Enter the search string : ")
# Append a directory separator if not already present
if not (search_path.endswith("/") or search_path.endswith("\\") ):
search_path = search_path + "/"
# If path does not exist, set search path to current directory
if not os.path.exists(search_path):
search_path ="."
# Repeat for each file in the directory
for fname in os.listdir(path=search_path):
# Apply file type filter
if fname.endswith(file_type):
# Open file for reading
fo = open(search_path + fname, 'r')
# Read the first line from the file
line = fo.read()
# Initialize counter for line number
line_no = 1
# Loop until EOF
if line != '' :
# Search for string in line
index = line.find(search_str)
if ( index != -1) :
print(bcolors.OKGREEN + '[+]' + bcolors.ENDC + ' ', fname, sep="")
print(' ')
sleep(0.01)
else:
print(bcolors.FAIL + '[-]' + bcolors.ENDC + ' ', fname, ' ', 'does not contain', ' ', search_str, sep="")
print(" ")
sleep(0.01)
line = fo.readline()
# Increment line counter
line_no += 1
# Close the files
fo.close()
就是这样!
答案 3 :(得分:0)
我正在尝试使用以下代码解决此类问题,请查看。
import os,sys
search_path=input("Put the directory here:")
search_str = input("Enter your string")
# Append a directory separator if not already present
if not (search_path.endswith("/") or search_path.endswith("\\") ):
search_path = search_path + "/"
# If path does not exist, set search path to current directory
if not os.path.exists(search_path):
search_path ="."
# Repeat for each file in the directory
for fname in os.listdir(path=search_path):
# Apply file type filter
if fname.endswith(file_type):
# Open file for reading
fo = open(search_path + fname)
# Read the first line from the file
line = fo.readline()
# Initialize counter for line number
line_no = 1
# Loop until EOF
while line != '' :
# Search for string in line
index = line.find(search_str)
if ( index != -1) :
print(fname, "[", line_no, ",", index, "] ", line, sep="")
# Read next line
line = fo.readline()
# Increment line counter
line_no += 1
# Close the files
fo.close()