泰山神 发表于 2018-8-12 06:18:21

python编写过滤指定字符的文本

#!/usr/bin/python  
#coding:utf-8
  

  
import os
  
import string
  
import sys, getopt
  

  
def Grep_start_char(Mpath, char):
  
    if os.path.exists(Mpath):
  
      GrepFile = file(Mpath)
  
    else:
  
      return "No such file or directory"
  

  
    try:
  
      for line in hosts:
  
            if line.startswith(char) or line.strip() == '':
  
                continue
  
            print line
  
    finally:
  
      hosts.close()
  

  
if __name__ == '__main__':
  

  
#opts,args = getopt.getopt(sys.argv,'')
  
    Mpath = raw_input('input file path: ')
  
    char = raw_input('input grep char: ')
  
    if Mpath and char:
  
      Grep_start_char(Mpath, char)
页: [1]
查看完整版本: python编写过滤指定字符的文本