stripping spaces in front of line
Peter Otten
__peter__ at web.de
Sat Mar 4 04:49:07 EST 2006
eight02645999 at yahoo.com wrote:
> f = open("textfile","rU")
> while (1):
> line = f.readline().strip()
> if line == '':
> break
> print line
> f.close()
Be warned that your code doesn't read the whole file if that file contains
lines with only whitespace characters. If you want to print every line,
change the loop to
while 1:
line = f.readline()
if line == "":
break
print line.strip()
i. e. don't strip() the line until you have tested for an empty string. The
idiomatic way to loop over the lines in a file is
for line in f:
print line.strip()
Peter
More information about the Python-list
mailing list