I use python 2.7.5.
I have got some files in the directory/sub directory. Sample of the file1 is given below
Title file name
path1 /path/to/file
options path2=/path/to/file1,/path/to/file2,/path/to/file3,/path/to/file4 some_vale1 some_vale2 some_value3=abcdefg some_value4=/path/to/value some_value5
I would like to insert the text /root/directory in the text file. Final outcome i would like to have is as followes:-
Title file name
path1 /root/directory/path/tofile
path2=/root/directory/path/to/file1,/root/directory/path/to/file2,/root/directory/path/to/file3,/root/directory/path/to/file4
options some_vale1 some_vale2 some_value3=abcdefg some_value4=/path/to/value some_value5
The names path, options and path2 are same in all files. The files in the directory/subdirectory required to be modified with the same outcome as above. I tried to use the re.sub to find and replace the string. However I never got the output i wanted.
re.subthat you have tried?re.sub(r"([ \t =,])/", replace_text, text)wherereplace_text=/root/directoryand text is the content of the file loaded with.read(). As of now i am just trying to figure out replacing/inserting. Getting path2 line in to next line is a different story.