1 #!/usr/bin/env python
2 # encoding: utf-8
3 # 读取中文,搜索中文
4 import os
5
6 def get_lines(fileName):#获取文件行数
7 count = 0
8 for index, line in enumerate(open(fileName,'rb')):
9 count += 1
10 return count
11
12 def loadDataSet(fileName, splitChar='\t'):#查看文件中某些行内容
13 """
14 输入:文件名
15 输出:数据集
16 描述:从文件读入数据集
17 """
18 dataSet = []
19 with open(fileName,'rb') as fr:
20 for line in fr.readlines()[0:2]:#要显示的行数
21 dataSet.append(line)
22 return dataSet
23
24 def search_records(fileName,txt, splitChar='\t'):#遍历文件夹下文件中的内容
25 dataSet = []
26 suc=1
27 with open(fileName, 'r',encoding='utf8') as fr:
28 #with open(fileName, 'rb') as fr:二进制格式读取 ,txt需要以二进制格式搜索,前面加b
29 for line in fr.readlines(): # 要显示的行数
30 if txt in line:
31 return suc,txt,line
32
33 def get_nums_of_folder(path):#获取文件夹下有多少文件夹(文件)
34 # 统计 E:/ 下的文件夹个数
35 #path = "E:"
36 count = 0
37 for fn in os.listdir(path): # fn 表示的是文件名
38 count = count + 1
39 return count
40
41 def get_nums_of_files(path):#获取文件夹下文件数
42 # 统计 E:/201903 下的文件个数
43 # path = os.getcwd() #获取当前路径
44 #path = "E:/201903"
45 count = 0
46 for root, dirs, files in os.walk(path): # 遍历统计
47 for each in files:
48 count += 1 # 统计文件夹下文件个数
49 return count # 输出结果
50
51 if __name__ == '__main__':
52 print("start:")
53 txt='SUPAY|3100016019220056|1342933****|#011/0MuFqJm7H81vyfw5v/30Q==' #查找内容
54 #print(loadDataSet('E:/SQL.txt'))
55 #print(get_lines('E:/201903/crm_usr_info_0.txt'))
56 #print(get_lines('E:/1.txt'))
57 #print(search_records('E:/1.txt'))
58 #print(loadDataSet('E:/201903/crm_usr_info_10.txt'))
59 count=get_nums_of_files('E:/201903')
60 for i in range(0,count):
61 print('E:/201903/crm_usr_info_%s.txt' % str(i))
62 suc=search_records('E:/201903/crm_usr_info_%s.txt' % str(i),txt)
63 print(suc)
64 if isinstance(suc,tuple) and suc[0]==1:
65 print(suc[1])
66 print(suc[2])
67 break
68 print("end:")