replace line #1 with "newline\n" in text file

Rony bucodi_no_spam at ahoo.fr
Wed Dec 24 05:51:02 EST 2003


Group : comp.lang.python

> Hiya,
> I am trying to replace the first line of a config file.. with a new string
> been googlizing and reading the python manual.. but can't find what I need..
> it's all rubbish about bits and bytes etc..
> 
> 
> 
Perhaps this is what you want ?

flag=0
newlines = []
lines = open('test.txt','r').readlines()
for l in lines:
  if flag==0:
    newlines.append('\n')
    flag=1
  else:
    newlines.append(l)
open('test.txt','w').writelines(newlines)


Rony
-- 
Rony

  /_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 /   bucodi_no_spam at yahoo.fr (delete _no_spam)
/
|    www.bucodi.com - My work
\    www.ifrance/karamusique  -- My hobby
 \_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/






More information about the Python-list mailing list