|
发表于 2020-12-1 13:56:50
|
显示全部楼层
本楼为最佳答案
你把问题想复杂了。直接在get_targets函数的if下添加上打开文件、读取文件,判断文件每行首字母即可,其他的不用动。
is_decoy使用递归多累啊,直接一个循环搞定。
- import os
- def is_decoy(lines):
- if not lines:
- return False
- for line in lines:
- if line[0] in ['A','C','M','E']:
- return True
- return False
- def get_targets(path):
- for file in os.listdir(path):
- if os.path.isfile(path+'/'+file):
- thisfile=open(path+os.sep+file,'r')
- filelines=thisfile.readlines()
- thisfile.close()
- if is_decoy(filelines):
- print(path+'/'+file) #This is a file, print out the path
- else:
- get_targets(path+'/'+file) #Go into a subdirectory
复制代码 |
|