[Tutor] write a file

John Purser johnp at HomeLumber.com
Wed Mar 24 15:04:30 EST 2004


Megan,

I'd guess you were reading a file created on windows under Linux/Unix.  When I run your code on a windows file under windows it works fine.  It would help if you included the platform you are using.  You might want to try:
n = file('a.txt', 'r')
o = file('b.txt', 'w')
for line in n.readlines():
    # print line
    o.write(line.rstrip() + '\n')
n.close()
o.close()

which will strip the white characters off the right side of the line.  Then the '\n' will put the correct line termination character for your system on it.

And I think file is the same as open.  I believe file is the new version and open links to it but I could have that backwards.

Good Luck,

John Purser

-----Original Message-----
From: APQ LE [mailto:apqle75 at hotmail.com]
Sent: Wednesday, March 24, 2004 12:13 PM
To: tutor at python.org
Subject: [Tutor] write a file


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()

==========
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.

~Megan

_________________________________________________________________
MSN Toolbar provides one-click access to Hotmail from any Web page - FREE 
download! http://toolbar.msn.com/go/onm00200413ave/direct/01/


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



More information about the Tutor mailing list