0

Badly need assistance on this:

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

for line in f:
    print(line, end='')
    if (line.startswith("path = ")) in f:
        line = CurrentFilePath + "\n" 
        f.write(line)
        print ("Success!!!!")
    else:
        print('Errorrrr!!!')

this line of code just reads the file, and as soon as the line which needs to be read and then replaced, it throws an error! Please help. Very very new to python.

syntonym
  • 7,134
  • 2
  • 32
  • 45
Vighnesh Pai
  • 1,795
  • 1
  • 14
  • 38

2 Answers2

2

Your problem is in your if statement:

if (line.startswith("path = ")) in f:

When you reach a line that start with "path = ", that line evaluates to:

if True in f:

...which doesn't really make sense. Files don't contain boolean values. Simply removing the in f will get you past that; it's not doing anything for you here.

glibdud
  • 7,550
  • 4
  • 27
  • 37
0

f is an iterator and in operator in for loop is made to call next() on it. What is in doing with if? Check the syntax for if. Delete in f from that line and hopefully it will work.

C Panda
  • 3,297
  • 2
  • 11
  • 11