By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,510 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Retaining Unix EOL when reading/writing in windows

P: n/a
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

Jan 18 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Nick Wain wrote:
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
Jan 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.