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,
- I'm unable to replace that line using following code
- 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!!!!")