python查找字符串所在行_python 2.7.5 获取文本关键字符所在行

#!/usr/bin/envpython

#-*- coding: UTF-8 -*-from __future__ import print_function

importtime,os,sys,re,subprocess,commands,itertools,readline

dt=time.localtime()

ft="%Y-%m-%d %H:%M:%S %p %A"

date=time.strftime(ft,dt)

print("当前系统时间为:\n",date)

#---------------------------------------------------------------------------------#f0= open('/etc/ssh/sshd_config','r')

#f1=f0.readline()

#def get_number():

#sshd_config= "/etc/ssh/sshd_config"#with open(sshd_config,'r') as f:

#for line inf.readlines():

#try:#if'Port' in line:#strlist= line.split('Port')

#for sshdport in strlist[1:]:

#print(sshdport)

#except:

#print('获取失败')#if __name__ == '__main__':#get_number()

#---------------------------------------------------------------------------------#f= open('/etc/ssh/sshd_config','r')

#lines=f.readlines()

#for line inlines:#if "Port " in line:#print(line)

#[root@localhost~]# python 1.py

#当前系统时间为:

#2019-10-10 23:27:03PM Thursday

#Port22#---------------------------------------------------------------------------------#yum install -y python python-devel

#

#Port22#AddressFamily any

#with open('/etc/ssh/sshd_config') as infile, open('/root/1.txt', 'w') as outfile:

#copy=False

#for line ininfile:#if line.strip() == "#":#copy=True#elif line.strip() == "#AddressFamily any":#copy=False#elif copy:#outfile.write(line)

#[root@localhost~]# cat 1.txt

#Port22#---------------------------------------------------------------------------------#pathfile= raw_input('文件绝对路径:')

#keyname= raw_input('查找内容:')

#FoundFlag=False

#f00=open(pathfile)

#f01=f00.readline()

#whilef01:#if f01.find(keyname) == 0:#FoundFlag=True

#print("---------------------------------------------------------------------------------")

#print("所在行:" + f01, end='')

#print("---------------------------------------------------------------------------------")

#break#else:#f01=f00.readline()

#f00.close()#if FoundFlag == False:#print("获取失败.....!")

#raw_input()

# 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0#[root@localhost~]# python 1.py

#文件绝对路径:/etc/ssh/sshd_config

#查找内容:#Port

#---------------------------------------------------------------------------------#所在行: #Port22#---------------------------------------------------------------------------------#[root@localhost~]# python 1.py

#文件绝对路径:/etc/ssh/sshd_config

#查找内容:22#获取失败.....!

#Traceback (most recent calllast):

#File"1.py", line 25, in #raw_input()

#KeyboardInterrupt

#[root@localhost~]# python 1.py

#文件绝对路径:/etc/ssh/sshd_config

#查找内容:Port22#获取失败.....!

#find模块查询只能从左到右

#---------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值