[Tutor] (no subject)
Kent Johnson
kent37 at tds.net
Thu May 15 17:51:58 CEST 2008
On Thu, May 15, 2008 at 11:22 AM, root <jatinder.singh2 at wipro.com> wrote:
> hi ,i am working to replace three lines in a httpd.conf file using
> regular expression
You don't need regex for this, you are replacing plain strings. I
would read the entire file, change the strings and write it out again.
For example,
path = "/root/Desktop/httpd.conf"
f = open(path)
data = f.read()
f.close()
for search, replace in [
('User apache ' , 'User myuser '),
('Group apache','Group myuser'),
('DirectoryIndex\ index.html\ index.html.var','DirectoryIndex\
index.html\ index.html.var\ login.html')
]:
data.replace(search, replace)
f = open(path, 'w')
f.write(data)
f.close()
This replaces the file in place, if you want to make a backup it will
be a little more complex.
A few other notes:
- don't use 'file' as a variable name, it shadows the builtin file() function
- I'm not sure what the \ in your search string are for but they are
interpreted as escape characters. If you want a literal \ in the
string you have to use two \\ or prefix the string with r to make a
raw string.
Kent
More information about the Tutor
mailing list