python 2.7.5 獲取文本關鍵字符所在行


#!/usr/bin/env python
#-*- coding: UTF-8 -*-
from __future__ import print_function
import time,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 in f.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 in lines:
    #if "Port " in line:
       #print(line)
       
#[root@localhost ~]# python 1.py
     #當前系統時間為:
     #2019-10-10 23:27:03 PM Thursday
#Port 22
#---------------------------------------------------------------------------------

#yum install -y python python-devel

#
#Port 22
#AddressFamily any

#with open('/etc/ssh/sshd_config') as infile, open('/root/1.txt', 'w') as outfile:
    #copy = False
    #for line in infile:
        #if line.strip() == "#":
            #copy = True
        #elif line.strip() == "#AddressFamily any":
            #copy = False
        #elif copy:
            #outfile.write(line)
#[root@localhost ~]# cat 1.txt
#Port 22

#---------------------------------------------------------------------------------
#pathfile = raw_input('文件絕對路徑:')
#keyname = raw_input('查找內容:')

#FoundFlag = False
#f00 = open(pathfile)
#f01 = f00.readline()
#while f01:
    #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
#---------------------------------------------------------------------------------
   #所在行: #Port 22
#---------------------------------------------------------------------------------

#[root@localhost ~]# python 1.py
#文件絕對路徑:/etc/ssh/sshd_config
#查找內容:22
#獲取失敗.....!
#Traceback (most recent call last):
  #File "1.py", line 25, in <module>
    #raw_input()
#KeyboardInterrupt
#[root@localhost ~]# python 1.py
#文件絕對路徑:/etc/ssh/sshd_config
#查找內容:Port 22
#獲取失敗.....!
#find 模塊查詢只能從左到右
#---------------------------------------------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM