zoukankan      html  css  js  c++  java
  • 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("     当前系统时间为:
    ",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 模块查询只能从左到右
    #---------------------------------------------------------------------------------
  • 相关阅读:
    Luogu P4716 【模板】最小树形图
    P4180 严格次小生成树[BJWC2010] Kruskal,倍增
    LA4080/UVa1416 Warfare And Logistics 最短路树
    LA4255/UVa1423 Guess 拓扑排序 并查集
    【算法竞赛入门经典—训练指南】学习笔记(含例题代码与思路)第三章:实用数据结构
    【算法竞赛入门经典—训练指南】学习笔记(含例题代码与思路)第二章:数学基础
    【算法竞赛入门经典—训练指南】学习笔记(含例题代码与思路)第一章:算法设计基础
    P4177 [CEOI2008]order 网络流,最小割,最大权闭合子图
    [USACO5.1] 乐曲主题Musical Themes
    [USACO06DEC] 牛奶模式Milk Patterns
  • 原文地址:https://www.cnblogs.com/huixst/p/11728621.html
Copyright © 2011-2022 走看看