-1

I'm trying to replace a string which will be in the form of path='/users/username/folder' in a text file. I'm reading that text file and searching the line which starts from 'path ='. Here I've two problems,

  1. I'm unable to replace that line using following code
  2. If that string starts in between then this code may not work as I'm checking line.startswith().

Please help.

f = open('/Volumes/Personal/example.text','r+')

for line in f:
    print(line, end='')
    if (line.startswith("path = ")):
        # You need to include a newline if you're replacing the whole line
        line = CurrentFilePath + "\n" 
        f.write(line)
        print ("Success!!!!")
Vighnesh Pai
  • 1,795
  • 1
  • 14
  • 38
  • You say `path=` pattern in question and you write `path = ` pattern in your code. – psiyumm Apr 13 '16 at 12:44
  • what do u want to replace the line that starts with path= with? – Keatinge Apr 13 '16 at 12:46
  • I'm reading different path [CurrentFilePath]and i want replace that in above code. – Vighnesh Pai Apr 13 '16 at 12:47
  • Recommended reading: [(1)](http://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python) [(2)](http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python) – glibdud Apr 13 '16 at 12:49

1 Answers1

2

You can use regular expression.

import re
with open("filename","r+") as f:
    text = f.read()
    modified_text, modified = re.subn(r'(?:^|(?<=\n))path\s\=.*',CurrentFilePath, text)
    if  modified:
        print ("Success!!!!")
    else:
        print ("Failure :(")
    f.seek(0)  
    f.write(modified_text)  
    f.truncate()
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
  • Could you please show me how to write above code using regex? – Vighnesh Pai Apr 13 '16 at 12:45
  • You may look at the updated code now, I hope it satisfies your demand. – Ahsanul Haque Apr 13 '16 at 12:53
  • Hey, Its doing like this: earlier it was path = /users/username/folder/ after this code its like, [the currentpath]/users/username/folder. this is not what i wanted – Vighnesh Pai Apr 13 '16 at 12:54
  • earlier it was like this: text junk text path = /dummy/dummy/dummy/ junk text junk text junk text junk text junk text junk text junk text junk text NOW, its like this: text junk text junk text junk text junk text junk text path = /dummy/dummy/dummy/ t junk text junk text junk text junk text junk text junk text junk text junk text text junk text junk text junk text junk text desc =/Users/admin/Desktop/Example folder/example.text junk text junk text junk text junk text junk text junk t – Vighnesh Pai Apr 13 '16 at 12:58
  • The path is getting updated, but its getting appending with previous data to the file. I need only latest thing with only path change. Pleas ehelp and this will be last – Vighnesh Pai Apr 13 '16 at 13:00
  • This is going to append the updated file contents to the end of the old file contents. You'll need to `seek` (and probably `truncate` in case the new contents are shorter than the old). Also note that this loads the entire file into memory, which may or may not be an issue depending on file size. – glibdud Apr 13 '16 at 13:24
  • @Vighnesh Pai, see if it is what you need. – Ahsanul Haque Apr 13 '16 at 13:55