我想在包含几个子目录(如'20150516')的目录'excercise'下的所有文件中搜索关键字。os.path.isfile(file_path)当file_path是相对路径时返回false,为什么?
这里是我的代码:()
import os,sys,view_all
def search_special(file):
with open(file,'r') as fp:
while 1:
line = fp.readline()
if len(line) == 0:
break
if 'KeyboardInterrupt' in line:
res.append(file)
break
if not (file in res):
print "%s has no keyword 'KeyboardInterrupt'"%file
def traver_path(main_dir):
for path_name in os.listdir(main_dir):
current_dir = os.path.abspath(main_dir)
recursive_dir = os.path.join(current_dir,path_name)
if os.path.isdir(recursive_dir):
traver_path(recursive_dir)
if os.path.isfile(recursive_dir):
if path_name[-3:] == '.py':
search_special(recursive_dir)
if __name__ == "__main__":
res = []
traver_path('.')
# print res
for item in res:
view_all.print_file(item)
而且效果很好。但是,如果我做一个小小的改变FUNC traver_path像:
def traver_path(main_dir):
for path_name in os.listdir(main_dir):
if os.path.isdir(path_name):
traver_path(os.path.join(os.path.abspath(main_dir),path_name))
if os.path.isfile(path_name):
if path_name[-3:] == '.py':
search_special(os.path.join(os.path.abspath(main_dir),path_name))
注意,对于os.path.isdir和os.path.isfile参数已经被改变(不再是ABSPATH)
我当我通过pdb调试时发现了一些有趣的东西。
(Pdb)
> /Users/Crayon_277/Develop/Project/Python/exercise/view_special.py(27)traver_path()
-> if os.path.isdir(path_name):
(Pdb) p path_name
'20150507'
(Pdb) n
> /Users/Crayon_277/Develop/Project/Python/exercise/view_special.py(28)traver_path()
-> traver_path(os.path.join(os.path.abspath(main_dir),path_name))
获取到子目录20150507
(Pdb) p path_name
'common_divisor.py'
(Pdb) n
> /Users/Crayon_277/Develop/Project/Python/exercise/view_special.py(29)traver_path()
-> if os.path.isfile(path_name):
(Pdb) s
--Call--
> /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py(26)isfile()
-> def isfile(path):
(Pdb) return
--Return--
> /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py(31)isfile()->False
-> return False
返回false,这应该是真实的,导致common_divisor.py 是文件。
另一项测试:
>>> for i in os.listdir('.'):
... print i,str(os.path.isfile(i))
...
.DS_Store True
.view_all.py.swp True
.view_special.py.swp True
20150506 False
20150507 False
20150509 False
20150510 False
20150511 False
20150512 False
20150513 False
20150514 False
20150516 False
view_all.py True
view_all.pyc True
view_special.py True
>>> for i in os.listdir('./20150509'):
... print i,str(os.path.isfile(i))
...
bibao.py False
chinese_test.py False
decorate.py False
encrypt.py False
isinstance_test.py False
python3_test.py False
我是来与比相对路径ABSPATH os.path.isfile更好的作品正确的结论?
你知不知道所有这些都可以通过简单地使用['grep'](http://linux.die.net/man/1/grep)来避免? – MattDMo
@MattDMo号但是grep是一个shell命令?你可以再详细一点吗? – MMMMMCCLXXVII
你也可以使用'os.walk()' –