[Tutor] Strings and file saving
Sean 'Shaleh' Perry
shaleh@valinux.com
Fri, 18 Aug 2000 12:20:02 -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)
> out_file.close()
> in_file.close()
a only exists in the if block, which out_file.write() is not in. This is
probably a logic error. Even if a did exist, if the if was not true, you would
write an empty line or worse, the last line in a.
To ensure a does exist do the following:
a = ""
if blah:
a = do_string_stuff
out_file.write(a)
The same idea is often used to make an empty list, dictionary, etc for a loop
to populate.