"Python Programming Quick Start" 8.9.3 practical exercises

Source: Internet
Author: User
#!python3# -*- coding:utf-8 -*-# 8.9.3#打开指定路径中所有.txt文件#用户输入正则,将匹配行输出,输出文件名import re,ospat=input("输入要处理的文件夹绝对路径:")lis_dir=os.listdir(pat)lis_txt=[]for x in lis_dir: #取出.txt结尾的文件名,加进列表 if re.search(r‘\.txt$‘,x): lis_txt.append(x)text=input("输入你的自定义正则表达式:")regCom=re.compile(text)#regCom=re.compile(‘.*name.*‘)#循环处理文件for fi in lis_txt: fi=os.path.join(pat,fi) fi_open=open(fi) for fi_line in fi_open.readlines(): fi_reg=regCom.search(fi_line) if fi_reg: print(fi_reg.group()) fi_open.close()print("查找完成")

"Python Programming Quick Start" 8.9.3 practical exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.