找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
#coding=utf-8
from time import *
import os
#1、使用os模块写一个递归调用打印出E:\\home下的所有文件名的绝对路径

def finddir(dir):
    r=os.listdir(dir)
   
#print(r)
   
for i in r:
         
#b=dir+'\\'+i
          #print(i)
         
if os.path.isdir(dir+'\\'+i):#b 如果是目录,则递归进入该文件
            
finddir(dir+'\\'+i)#b
         
else:        #不是目录那就是文件,打印该文件绝对路径
            
print(dir+'\\'+i)#b
a=time()
finddir(
'E:\python')
print("检索所有文件所用时间(单位/秒):",time()-a)  #'E:\python'平均用时0.31


import re
#2、用正则方法实现统计E:\\python文件中
#指定字符如"python"的行数?(文件中的python字符)

with open(r"E:\home\123.txt",'r',encoding='utf-8') as f:
   a=f.readlines()
   c=
0
   
listc=[]
   
for i in a:
       c=c+
1
      
if re.findall("python",i):
           
print("第%d行有python字符"%(c))
           listc.append(c)
   
print("这些行有python字符",listc)

#3、使用正则完成市面上手机规则的编写
# 随机生成11位数字然后通过正则匹配出符合规则的11为数手机号码?
import string
from random import *
import re
def yzm():#11位数字 (int型)
    #a=1*10**10
    #print(a)
    #b=int(random()*10**10)
    #print(b)
   
dd=str(1*10**10+int(random()*10**10))#第一位锁定为1,就不怕随机到0了
   
#print(dd)
   
if re.match("^1[3579][0-9]\d{8}",dd):
        
print(re.match("^1[3579][0-9]\d{8}",dd).group())
   
else:
        yzm()
yzm()

#4、用正则实现写一段代码统计
# E:\\log文件中error和warning单词出现的次数分别为几次?
with open(r"E:\home\456.txt",'r',encoding="utf-8") as log:
    a=log.readlines()
    ec=
0
   
wc=0
#方法一,用到count函数
    # for i in a:
    #     #print(i)
    #     if re.match("\D+error",i) or re.match("error",i):
    #        vv=i.count("error")
    #        ec=ec+vv
    #     if re.match("\D+warning",i) or re.match("warning",i):
    #        vb=i.count("warning")
    #        wc=wc+vb
#方法二,完全脱离count函数
   
for i in a:
        
if re.findall("error",i) :
            
#aaa=re.findall("error",i)
            #print(aaa)
            
ec=ec+len(re.findall("error",i))#aaa
        
if re.findall("warning",i) :
            
#bbb=re.findall("warning",i)
            #print(aaa)
            
wc=wc+len(re.findall("warning",i))#bbb
   
print('error的个数:',ec)
   
print('warning的个数',wc)




分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册