[Tutor] file I/O

Massey, Craig Craig.Massey@oxygenforbusiness.com
Thu, 12 Jul 2001 09:00:29 +1200


Well there you go, thanks. I did a workaround which got there, but I was
looking for something neater like this. I'd love to know how people find out
about these things as I spent a lot of time looking for it. The help with
Activestate is either babyish(Tutorial) or very specific (Language
Reference) and I have trouble finding out how to do things.

Ignore me, I'm a newbie struggling up the learning curve on another language
and not liking it any better this time either. Once I stop feeling stupid
because I don't know how to do stuff like this I'll be happier.

-----Original Message-----
From: sill@optonline.net [mailto:sill@optonline.net]
Sent: Thursday, July 12, 2001 8:48 AM
To: tutor@python.org
Subject: Re: [Tutor] file I/O


On Thu, Jul 12, 2001 at 08:35:39AM +1200, Massey, Craig wrote:
> Is there a "standard" way of removing the newline, like chomp in Perl?

there's strip() method:

>>> s = 'aljsk\n'
>>> s
'aljsk\n'
>>> print s
aljsk

>>> s.strip()
'aljsk'

>>> help(s.strip)
Help on built-in function strip:

strip(...)
    S.strip() -> string
        
        Return a copy of the string S with leading and trailing
        whitespace removed.

> 
> -----Original Message-----
> From: Michael P. Reilly
> [mailto:arcege@dsl092-074-184.bos1.dsl.speakeasy.net]
> Sent: Thursday, July 12, 2001 8:31 AM
> To: kp87@lycos.com
> Cc: tutor@python.org
> Subject: Re: [Tutor] file I/O
> 
> 
> kevin parks wrote
> > 
> > Hi. I am a little confused as to why this hangs when i run it. I am
trying
> to
> 
> Off hand, I would say it is the end condition.  The readline method
> returns an empty string (false) when the end of file is reached, not a
> string with a space.
> 
> > 	done = 0
> > 	while not done:
> > 		aLine = infile.readline()
> > 		if aLine != " ":
>                 if aLine != "":  # not at EOF, so write out
> 
> > 			f.write(aLine + '\n')
> > 		else:
> > 			done = 1
> 
> Also, readline returns a whole line of text, including the newline.
> You probably do not need to append an additional newline.
> 
>   -Arcege
> 
> -- 
> +----------------------------------+-----------------------------------+
> | Michael P. Reilly                | arcege@speakeasy.net              |
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> DISCLAIMER:  This electronic message together with any attachments is 
> confidential.  If you are not the intended recipient, do not copy,
disclose or 
> use the contents in any way.  Please also advise us by return e-mail that
you 
> have received the message and then please destroy.  Carter Holt Harvey is
not 
> responsible for any changes made to this message and / or any attachments
after 
> sending by Carter Holt Harvey.  We use virus scanning software but exclude
all 
> liability for viruses or anything similar in this email or any attachment.
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

DISCLAIMER:  This electronic message together with any attachments is 
confidential.  If you are not the intended recipient, do not copy, disclose or 
use the contents in any way.  Please also advise us by return e-mail that you 
have received the message and then please destroy.  Carter Holt Harvey is not 
responsible for any changes made to this message and / or any attachments after 
sending by Carter Holt Harvey.  We use virus scanning software but exclude all 
liability for viruses or anything similar in this email or any attachment.