您好我编写了此代码,用于在文件列表中搜索关键字
import glob
import os
print "enter directory path"
path=raw_input()
print "Enter keyword"
key=raw_input()
os.chdir(path)
for files in glob.glob("*.*"):
with open(file) as f:
contents=f.read()
if key in contents:
print file
我对Python比较陌生。任何人都可以帮我修改同样的子目录搜索吗?
答案 0 :(得分:0)
使用os.walk:
for root, dirs, files in os.walk(your_dir_path):
for file in files:
file = os.path.join(root, file)
with open(file) as f:
...