|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
是不是我哪里有问题,导致最后一步,无法调用函数,一调用就出错
import os
def pos_in_line (line,key):
pos= []
begin = line.find(key,0)
if begin != -1:
pos.append (begin+1)
begin = line.find(key,begin+1)
return pos
def pos_in_file (file , key):
f = open (file)
count = 0
key_dict= dict()
for each_line in file:
count +=1
key_dict[count] = pos_in_line (each_line ,key)
f.close()
return key_dict
def search_files(key,path):
txt_files=[]
dict1= dict()
for root,dirs, files in os.walk(path):
for name in files:
if os.path.splitext[1]== '.txt':
txt_name = os.path.jion(root,name)
txt_files.append (text_name)
for file in txt_files:
dict1 = pos_in_file (file , key)
num = dict1.key()
num = sorted(num)
for each in num:
print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num])
search_files('爱','G:/python/测试/30.4')
print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num]) )
少了个括号,补上就行了
- import os
- def pos_in_line (line,key):
- pos= []
- begin = line.find(key,0)
- if begin != -1:
- pos.append (begin+1)
- begin = line.find(key,begin+1)
- return pos
- def pos_in_file (file , key):
- f = open (file)
- count = 0
- key_dict= dict()
- for each_line in file:
- count +=1
- key_dict[count] = pos_in_line (each_line ,key)
- f.close()
- return key_dict
- def search_files(key,path):
- txt_files=[]
- dict1= dict()
- for root,dirs, files in os.walk(path):
- for name in files:
- if os.path.splitext[1]== '.txt':
- txt_name = os.path.jion(root,name)
- txt_files.append (text_name)
- for file in txt_files:
- dict1 = pos_in_file (file , key)
- num = dict1.key()
- num = sorted(num)
- for each in num:
- print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num]))
- search_files('爱','G:/python/测试/30.4')
复制代码
|
|