I'm an idiot
David LeBlanc
whisper at oz.net
Fri Jun 28 22:30:12 EDT 2002
> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of David
> Sent: Friday, June 28, 2002 18:00
> To: python-list at python.org
> Subject: I'm an idiot
>
>
> OK, I am the first to admit it. I am an idiot. I have RTFM on this over
> and over, and I can still not figure out what I am doing wrong.
>
> I think the intent of the code is obvious, but just to clarify, I want to
> read every line in a file and write those lines back out to another file,
> but with leading and training space removed. I also want to have some
> elegant way to determine that I have reached the end of the file and
> break out of the loop.
>
> And just to explain my stupidity, my reference langauge is BASIC. I
> could have written this in BASIC in a minute, but I need to learn
> something new. Any help would be appreciated.
>
> David
>
>
> f=open('c:\\temp\\temp.txt', 'r')
> g=open('c:\\temp\\temp1.txt', 'w')
> while 1:
> try:
> s=f.readline
> g.write(s.split())
> except IOError:
> break
>
> g.close
> f.close
Here's my version that also strips out blank lines:
#lineio.py
f=open('j:/python22/lineio.py', 'r')
g=open('j:/python22/lineio.txt', 'w')
for s in f:
if s == '\n':
continue
g.write(s.strip() + '\n')
g.close()
f.close()
If you want to keep blank lines:
#lineio.py
f=open('j:/python22/lineio.py', 'r')
g=open('j:/python22/lineio.txt', 'w')
for s in f: g.write(s.strip() + '\n')
g.close()
f.close()
Dave LeBlanc
Seattle, WA USA
More information about the Python-list
mailing list