Advertisement
wagner-cipriano

FileLineFilter, file read, file write, with

Oct 12th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. from __future__ import print_function      #Compatibilidade func print python 2/3
  2. from os.path import isfile
  3.  
  4. def FileLineFilter(StrFilter, FileIn, FileOut):
  5.     #Remover as linhas que iniciem ou teminem com determinada expressão
  6.     with open(FileIn, 'r') as inf, open(FileOut, 'w') as ouf:
  7.         for LineStr in inf:
  8.             if (not LineStr.startswith(StrFilter)) and (not LineStr.endswith(StrFilter + '\n')):
  9.                 ouf.write(LineStr)
  10.     #python close files here
  11.  
  12. #File names and filter
  13. StrFilter = '#'
  14. FileIn = 'FileNameIN.txt'
  15. FileOut = 'FileNameOut.txt'
  16.  
  17. #Validate files
  18. assert isfile(FileIn), 'Arquivo de entrada (%s) nao existe. Verifique.' %(FileIn)
  19. assert not isfile(FileOut), 'Arquivo de saida (%s) ja existe. Verifique.' %(FileOut)
  20.  
  21. FileLineFilter(StrFilter, FileIn, FileOut)
  22. with open(FileOut, 'r') as f:
  23.     print(f.read())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement