[Noud Aldenhoven]
#- Hello everyone,
#-
#- I was wondering how to remove comments away form a file.
I remade it:
--------------------------------
#!/usr/bin/env python
import sys
import time
helptext = "usage: python rmcomment <oldfile> <newfile> <comment>"
def rmcomment(oldfilename, newfilename, comment):
start = time.time()
oldfile = file(oldfilename, 'r')
newfile = file(newfilename, 'w')
ccount = 0
lcount = 0
for lin in oldfile:
pos = lin.find(comment)
if pos != -1:
lin = lin[:pos] + "\n"
ccount += 1
newfile.write(lin)
lcount += 1
print "Done... in %.2f seconds\nRemoved comment from %d lines\nWrote %d lines t
o %s" % (time.time()-start , ccount, lcount, newfilename)
if __name__ == "__main__":
if sys.argv[1] == "-h" or sys.argv[1] == "-help":
print helptext
else:
oldfile = sys.argv[1]
newfile = sys.argv[2]
comment = sys.argv[3]
rmcomment(oldfile, newfile, comment)
--------------------------------
Some issues:
- Used <> instead of [] in the help text, as square brackets mean optional arguments.
- Eliminated the raw_input, as in these kind of scripts the user *never* expect to have interact input.
- Used file instead of open, to don't read the whole file in memory.
But the real change is inside the for loop.
Testing it:
[EMAIL PROTECTED] ~> cat ww
Hello Fuckin' World //how are you doing today
//I think it delete this sentence and the next sentence too!
But this one not! #Even not this comment
[EMAIL PROTECTED] ~> python pru.py ww w2 //
Done... in 0.00 seconds
Removed comment from 2 lines
Wrote 7 lines to w2
[EMAIL PROTECTED] ~> cat w2
Hello Fuckin' World
But this one not! #Even not this comment
[EMAIL PROTECTED] ~>
Regards,
. Facundo
Bitácora De Vuelo: http://www.taniquetil.com.ar/plog
PyAr - Python Argentina: http://pyar.decode.com.ar/
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ADVERTENCIA.
La información contenida en este mensaje y cualquier archivo anexo al mismo, son para uso exclusivo del destinatario y pueden contener información confidencial o propietaria, cuya divulgación es sancionada por la ley.
Si Ud. No es uno de los destinatarios consignados o la persona responsable de hacer llegar este mensaje a los destinatarios consignados, no está autorizado a divulgar, copiar, distribuir o retener información (o parte de ella) contenida en este mensaje. Por favor notifíquenos respondiendo al remitente, borre el mensaje original y borre las copias (impresas o grabadas en cualquier medio magnético) que pueda haber realizado del mismo.
Todas las opiniones contenidas en este mail son propias del autor del mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones Personales S.A. o alguna empresa asociada.
Los mensajes electrónicos pueden ser alterados, motivo por el cual Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación cualquiera sea el resultante de este mensaje.
Muchas Gracias.
-- http://mail.python.org/mailman/listinfo/python-list