[Tutor] Re: write a file

APQ LE apqle75 at hotmail.com
Wed Mar 24 16:51:47 EST 2004

'rU' works better, but not completely.  Now, I get two trailing question 
marks at the end of every line rather than an additional line with only 
question marks.

what is 'U'?


>From: Andrei <project5 at redrival.net>
>To: tutor at python.org
>Subject: [Tutor] Re: write a file
>Date: Wed, 24 Mar 2004 21:25:56 +0100
>Karl Pflästerer wrote on Wed, 24 Mar 2004 20:52:38 +0100:
> >> I'm trying to open a file, read its content and write its content to a
> >> new file.  Pretty much like copying a file.  Here is what I have
> >
> >> ========
> >> in = open ("a.txt", "r")
> >> out = open ("b.txt", "w")
> >> for line in in.readlines()
> >>    out.write(line)
> >
> >> in.close()
> >> out.close()
>That code works?? Never gives stuff names that are already used by Python.
>'in' is a kewyword used in e.g. 'for something in whatever', so you
>shouldn't be able to name a file 'in'. It's also a bad idea to override
>built-ins, like range, file or open - even though you don't do that in that
>particular piece of code :).
> >> ==========
> >> However, the output isn't as expected.  There is additional empty line
> >> comes after every existing line.  So, the output file is bigger than
> >> the original file.
>That might happen especially on Windows where lines end in '\r\n'. Let's
>see if I get this right... '\n' gets expanded to '\r\n' as well and '\n'
>means on its own that a new line is required, so you end up with an extra
>empty line. The line ends are very pesky, because different platforms use
>different line endings (on Linux lines end in \n, on Mac I believe in \r
>and on Windows in both).
>I think that if you open the file with "rU" instead of "r" you might get
>better results. (not available in Pythons older than 2.3)
>Real contact info (decode with rot13):
>cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
>gur yvfg, fb gurer'f ab arrq gb PP.
>Tutor maillist  -  Tutor at python.org

Get tax tips, tools and access to IRS forms – all in one place at MSN Money! 

More information about the Tutor mailing list