replacing text inplace
Grant Edwards
grante at visi.com
Sun Apr 20 20:07:28 EDT 2008
On 2008-04-20, Matt Herzog <msh at blisses.org> wrote:
> I'm learning some python with the seemingly simple task of
> updating a firewall config file with the new IP address when
> my dhcpd server hands one out. Yeah, I know it's dangerous to
> edit such a file "in place"
I don't see how what you're doing is editing a file "in place".
> but this is just an exercise at this point. I would not mind
> using file handles except they seem to add complexity.
Do you mean file objects?
> The only apparent problem I have with my script so far is that
> it's adding lots of blank lines to the file when it updates
> the IP addresses.
When you do this:
for line in inputfile:
Each instance of 'line' has a newline at the end of it.
When you do this:
print line
The print operation adds another newline. Try it this way:
print line,
The comma tells print not to append a newline after it has
printed "line".
> So "71.146.250.258" needs to change to "71.146.250.223" or something similar.
>
> Is there a saner, cleaner way to do this that won't add new,
> blank lines?
sed -i s/71.146.250.258/71.146.250.223/g filename
I suppose one probably should escape the dots in the input
regex...
--
Grant Edwards grante Yow! With YOU, I can be
at MYSELF ... We don't NEED
visi.com Dan Rather ...
More information about the Python-list
mailing list