Borrar un linea de un archivo

Chema Cortes py en ls-l.org
Lun Jul 18 11:53:18 CEST 2005


Chema Cortes escribió:

> Te propongo unas alternativas:
> 
> 1.-
> 
>   reglas=[]
>   for regla in open("/etc/firewall/gshield.last","r"):
>     if not ip in regla:
>       reglas.append(regla)
> 
> 
> 2.-
> 
>   reglas=[regla in open("/etc/firewall/gshield.last","r")
>          if ip not in regla]
> 
> 
> 3.- lo mismo que la 2, pero con generadores. Es una forma mucho mejor
> cuando se trabaja con ficheros enormes.
> 
>   reglas=(regla in open("/etc/firewall/gshield.last","r")
>          if ip not in regla)


Estas dos últimas son incorrectas. Deberían ser:

2.-

  reglas=[regla for regla in open("/etc/firewall/gshield.last","r")
          if ip not in regla]


3.- lo mismo que la 2, pero con generadores. Es una forma mucho mejor
cuando se trabaja con ficheros enormes.

  reglas=(regla for regla in open("/etc/firewall/gshield.last","r")
          if ip not in regla)




Más información sobre la lista de distribución Python-es