这只会产生一小部分匹配的项目列表,当有更多匹配的项目时。 findit包含转换为小写的搜索字符串。文件列表也会转换为小写。
home_dir_path = "/home/share/DUMMY_DATA" # Development data
full_path_original = ""
regex_string = "^" + home_dir_path + "/"
# Enter all or part of the file name to search:
if submit_search is not None:
print "<h1>Searching for: " + findit + "</h1>"
# Convert search_string to lower case for compare.
search_string = findit.lower()
for path, directories, files in os.walk(home_dir_path):
for file in files:
full_path_original = os.path.join(path, file)
full_path_display = re.sub(regex_string, "", full_path_original)
full_path = full_path_display.lower()
if search_string in full_path:
print full_path_display + "<br>"
如果我在最后一个if语句之前打印full_path,它会列出每个文件,所以看起来if search_string是不行的。但它匹配了几十个应该显示的数百个。
我是否应该使用另一种方法而不是&#39;如果full_path中的search_string:&#39; ?
以下是我为了重现问题而整理的一些示例数据。
find . | grep -i alpha
./alpha
./alpha/resume
./alpha/a
./alpha/b
./alpha/c
./alpha/d
./alpha/e
./alpha/f
./alpha/.hidden_file_test
./Alpha
./alphA
./alphA/stuff
./AlPha
./AlPha/.hidden_nothing
以上代码用于搜索&#34; alpha&#34;在与上面相同的目录中返回:
Searching for: alpha
alpha/f
alpha/f
alphA/stuff
AlPha/.hidden_nothing
AlPha/.hidden_nothing
AlPha/.hidden_nothing
它是那里的一个子集,它包含重复项。