(使用python3执行 python2会报错)
File "testc2.py", line 6
SyntaxError: Non-ASCII character '\xe5' in file testc2.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for detail
解释下:我没学过python,这是根据网上大神代码改写的,花了一天时间粗略的看了下python;
这里也是为了自己以后用
import os, os.path import sys def search(path, str): #如果文件存在 fp为文件路径 for x in os.listdir(path): fp = os.path.join(path, x) #如果文件存在,返回true if os.path.isfile(fp): #with as读取数据,将内容赋给fc 这里的xml和pa的值是为了更精确查找想要的文件(紫色行) 找到文件后可以通过print(line)输出指定字符串所在的行内容(红色行) xml = '.xml' pa = 'trunk/config' # if pa in fp: with open(fp, 'r',encoding='ISO-8859-1') as fc: for line in fc.readlines(): #如果匹配字符串成功 打印路径 if str in line: if pa in fp: if xml in fp: print(line) break elif os.path.isdir(fp): search(fp, str) #if len(sys.argv) == 1: # print ('useage: search str') #elif len(sys.argv) == 2: # str = sys.argv[1] # search('.', str) #else: # print('too many parameters') if __name__ == '__main__': search('你要搜索的文件夹', '想要查找的字符串')