Monday, May 21, 2012

Edit all files in subdirectories with python

I am trying to recursively loop through a series of directories (about 3 levels deep). In each directory is a series of text files, I want to replace a line of text with the directory path if the line contains a certain string so for example.



/path/to/text/file/fName.txt


If a line in fName in fName.txt text contains the string 'String1' I want to replace this line with 'some text' + file where file is the last part of the path.



This seems like it should be easy in python but I can't seem to manage it.



Thanks.





No comments:

Post a Comment