[Tutor] Strings and file saving
Daniel Yoo
dyoo@hkn.EECS.Berkeley.EDU
Fri, 18 Aug 2000 13:03:34 -0700 (PDT)
> # read each line in the file
> for line in in_file.readlines():
> if string.find(line,'$')>= 0:
> # # "print line" works well
> # #print line
> a = line
> # # write the new file you created to the file "junk2.txt"
> out_file.write(a)
The error is one of indentation --- you have the
out_file.write(a)
as a statement after the if block. This means that it'll try to write
that line, regardless if it found a line. Let's say that our first line
didn't have a '$'. We'll pass right by that if statement, and...
> Traceback (innermost last):
> File "today6.py", line 19, in ?
> out_file.write(a)
> NameError: a
Good luck to you.