|
5鱼币
各位大佬,请问为什么会出现这样的情况?
23、24行报错:invalid syntax
代码如下:
def key_find(each_line,key,count):
if count>0:
t = each_line.index(key)
position.append(t)
key_search(each_line[t+len(key):],key,count-1)
def result_find(path,each_file,key):
f = open(path+'\\'+each_file)
content = []
for each_line in f:
content.append(each_line)
for each_line in content:
if key in each_line:
print('在文件【%s】中找到关键字【%s】' %(path+'\\'+each_file,key))
count = each_line.count(key)
if count == 1:
position = each_line.index(key)
print('关键字出现在第 %d 行,第 %d 个位置' %(content.index(each_line)+1,position))
else:
position = []
key_search(each_line,key,count)
print('关键字出现在第 %d 行,第 %s 个位置' %(content.index(each_line)+1,str(position))
def goalfile_find(path,key):
all_file = os.listdir(path)
print('='*10)
for each_file in all_file:
(name,ext) = os.path.splitext(path+'\\'+each_file)
if ext == extension:
result_find(path,each_file,key)
if os.path.isdir(path+'\\'+each_file):
goalfile_find(path+'\\'+each_file,key)
import os
import os.path
extension = '.txt'
key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
choose = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' %key)
if choose in ['YES','Yes','yes']:
path = os.getcwd()
goalfile_find(path,key)
新手求解答和解决办法,谢谢
def key_find(each_line,key,count):
if count>0:
t = each_line.index(key)
position.append(t)
key_search(each_line[t+len(key):],key,count-1)
def result_find(path,each_file,key):
f = open(path+'\\'+each_file)
content = []
for each_line in f:
content.append(each_line)
for each_line in content:
if key in each_line:
print('在文件【%s】中找到关键字【%s】' %(path+'\\'+each_file,key))
count = each_line.count(key)
if count == 1:
position = each_line.index(key)
print('关键字出现在第 %d 行,第 %d 个位置' %(content.index(each_line)+1,position))
else:
position = []
key_search(each_line,key,count)
print('关键字出现在第 %d 行,第 %s 个位置' %(content.index(each_line)+1,str(position)))
def goalfile_find(path,key):
all_file = os.listdir(path)
print('='*10)
for each_file in all_file:
(name,ext) = os.path.splitext(path+'\\'+each_file)
if ext == extension:
result_find(path,each_file,key)
if os.path.isdir(path+'\\'+each_file):
goalfile_find(path+'\\'+each_file,key)
import os
import os.path
extension = '.txt'
key = input('请将该脚本放于待查找的文件夹内,请输入关键字:')
choose = input('请问是否需要打印关键字【%s】在文件中的具体位置(YES/NO):' %key)
if choose in ['YES','Yes','yes']:
path = os.getcwd()
goalfile_find(path,key)
|
|