|
本帖最后由 小数点儿 于 2018-3-1 14:05 编辑
新人第一天注册,第一次回帖
不废话,直接上代码- import os
- import re
- def file_name(file_dir):
- l=[]
- for root,dirs,files in os.walk(file_dir):
- for file in files:
- if os.path.splitext(file)[1] == ".txt":
- l.append(os.path.join(root,file))
- return l
- for i in file_name(r"C:\Users\test10\Desktop\BackUp"):
- with open(i, encoding='utf-8') as a_file:
- for a_line in a_file:
- m=re.match('^GigabitEthernet',a_line)
- if m is not None:
- with open(r"C:\Users\test10\Desktop\a.txt", mode='a', encoding='utf-8') as a_filew:
- a_filew.write(os.path.basename(i) +' '+ a_line)
复制代码- import os
- import re
- class lianxi:
- def __init__(self,ext,yuan,mubiao):
- self.ext=ext
- self.yuan=yuan
- self.mubiao=mubiao
- def file_name(self):
- return [os.path.join(root, file) for root, dirs, files in os.walk(self.yuan) for file in files if
- os.path.splitext(file)[1] == self.ext]
- def chulifile(self):
- for i in self.file_name():
- with open(i, encoding='utf-8') as a_file, open(self.mubiao, mode='a',
- encoding='utf-8') as a_filew:
- for a_line in a_file:
- m = re.match('^GigabitEthernet', a_line)
- if m is not None:
- a_filew.write(os.path.basename(i) + ' ' + a_line)
- lx=lianxi(".txt",r"C:\Users\test10\Desktop\BackUp",r"C:\Users\test10\Desktop\a.txt")
- lx.mubiao=r"C:\Users\test10\Desktop\b.txt"
- lx.chulifile()
复制代码
|
|