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

B

bmgz

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..
 
E

Erik Max Francis

bmgz said:
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..

You'll have to read in the file, write out a temporary one, and then
move the new one over the old one.
 
R

Rony

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

/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
/ (e-mail address removed) (delete _no_spam)
/
| www.bucodi.com - My work
\ www.ifrance/karamusique -- My hobby
\_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
J

Jeremy Kauffman

bmgz said:
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..

something like:

lines = open('config.txt', 'r').readlines()
lines[0] = 'newline\n'
open('newconfig.txt', 'w').writelines(lines)
 
A

Alex Martelli

bmgz said:
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..

Right -- files are in fact made of bits and bytes, "replacing a line" (with
one which has a different number of bytes) is not an existing basilar
operation on today's filesystems.

Nevertheless, the fileinput module of Python's standard library lets you
simulate "in-place changes", line by line, in textfiles, quite simply.


import fileinput

for line in fileinput.input(['thefile.txt'], inplace=True):
if fileinput.isfirstline():
print "the alternative line goes here"
else:
print line, # just pass it through -- note the comma!


Alex
 
A

Alex Martelli

Erik said:
You'll have to read in the file, write out a temporary one, and then
move the new one over the old one.

Yes, that's basically what fileinput (which I suggested in my other
msg in this thread) does for you behind the scenes. Of course, such
complication is typically only worth it for potentially big files --
for a small file, one might prefer a "rewrite-in-place" approach, e.g.:

f = file('thefile.txt', 'r+')
lines = f.readlines()
lines[0] = "the new line goes here\n"
f.seek(0)
f.writelines(lines)
f.close()

This should be fine unless the file is huge OR you're unlucky enough
that the machine crashes right while you're rewriting the file...;-)


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top