Advertisement
Sergio_Istea

search cadena 2

May 11th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. import sys
  4. import os
  5.  
  6. fichero = sys.argv[1]
  7.  
  8. if fichero == "--help":
  9.  
  10. print(f"Modo de uso:\n\t{sys.argv[0]} /ruta/al/fichero cadena\n")
  11. exit()
  12.  
  13.  
  14. if not os.path.exists(fichero):
  15.  
  16. print("No existe el fichero o directorio")
  17. exit(1)
  18.  
  19. cadena = sys.argv[2]
  20.  
  21. with open(fichero, 'r') as objeto_archivo:
  22.  
  23. lista = list(objeto_archivo)
  24.  
  25.  
  26.  
  27. indice = 0
  28.  
  29. list_size = len(lista)
  30.  
  31.  
  32. while indice < list_size:
  33.  
  34.  
  35. if cadena in lista[indice]:
  36.  
  37. print(f"El patron '{cadena}' se encuentra en el fichero")
  38. print(lista[indice])
  39.  
  40. indice += 1
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement