Retaining Unix EOL when reading/writing in windows

N

Nick Wain

Hello All,

I'm relatively new to PYTHON, using PYTHON 2.4 on Windows XP. I'm having a
problem as below. I've asked some other people in my office who are more
experienced in PYTHON, but they can't help.

I have a number of files created in UNIX that have the UNIX end of line
(EOL) character.

I want to read these files in python, modify some lines, and then write them
to a new file. This appears to work fine, however I find that the output
files have Windows EOL characters. This is a pain, as I want to compare the
before and after files to see if my changes are correct.

I've simplified my code down to something that just reads a file and then
writes it to a different file. I'm currently doing this with a bit of code
that looks something like this:

filename = "test.lwc"
outfile = open("test_out.lwc", 'w' )
readfile = open(filename,'r').readlines()

for line in readfile:
outfile.write(line)
outfile.close()

Is there a way I can do this, but retain the UNIX EOL characters?

Cheers,
Nick

_________________________________________________________________
Are you using the latest version of MSN Messenger? Download MSN Messenger
7.5 today! http://messenger.msn.co.uk
 
D

Diez B. Roggisch

Nick said:
I'm relatively new to PYTHON, using PYTHON 2.4 on Windows XP. I'm having a
problem as below. I've asked some other people in my office who are more
experienced in PYTHON, but they can't help.

I have a number of files created in UNIX that have the UNIX end of line
(EOL) character.

I want to read these files in python, modify some lines, and then write
them to a new file. This appears to work fine, however I find that the
output files have Windows EOL characters. This is a pain, as I want to
compare the before and after files to see if my changes are correct.

I've simplified my code down to something that just reads a file and then
writes it to a different file. I'm currently doing this with a bit of code
that looks something like this:

filename = "test.lwc"
outfile = open("test_out.lwc", 'w' )
readfile = open(filename,'r').readlines()

for line in readfile:
outfile.write(line)
outfile.close()

Is there a way I can do this, but retain the UNIX EOL characters?

Try using the 'b'-flag as filemode.

filename = "test.lwc"
outfile = open("test_out.lwc", 'wb' )
readfile = open(filename,'rb').readlines()

for line in readfile:
outfile.write(line)
outfile.close()

Not sure how well that plays with readline, as I have no Windows machine at
hand.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top