Python convert Windows CRLF to Mac CR ( line feeds)

Hi there

Does anyone know how to write a Python script that will convert all occurrences of a Windows carriage return/line feed in a document to Mac carriage returns?

Thanks in advance and Merry Christmas!

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If by "document" you mean "text file", this should do it.

Merry Christmas to you too!

import sys
while line:

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James MurrellProduct SpecialistCommented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Superdave's comment http:/Q_27507427.html#37331956

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
butterhookAuthor Commented:
I'd like to accept the answer, but can't seem to do so... any ideas?
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference ithrough the Colors of STEM program.

However, if you convert the text file from Windows on your Mac, it is probably better to open it as a text file using the universal line ending convertsion and then to write it back to another text file open for writing.

Another approach is to read whole content in binary mode, delete all \n characters, and write it back.
You can try the following scripts.  The first is to be run on the Mac target system to convert whatever line endings to the Mac ones.  (It can be run also on Windows to convert the line endings to the Windows ones, or on Unix to convert them to Unix ones.
import shutil

def convertLineEndings(fname):
    fnamebak = fname + '.bak'
    shutil.copy2(fname, fnamebak)  # just to be safe a bit
    # Open for reading in text mode with universal line endings.  Read 
    # the content of the whole file.  This is fine if the file is not
    # extremely big.  Then close the file.
    f = open(fname, 'Ur')  
    content =
    # Open the same file again, now for writing.  The line endings were
    # already converted during reading the content.  Just write it back.
    f = open(fname, 'w')
if __name__ == '__main__':

Open in new window

The following script can be run on whatever OS to convert the file line endings to the Mac ones (i.e. the CR only).
def convertLineEndingsForMac(fname):
    fnameresult = 'forMac' + fname
    # Open for reading in binary mode.  Read the content of the whole file.
    f = open(fname, 'rb')  
    content =
    # Open the target file for writing in binary. Find, if there are some CR's.
    # If yes, remove all the LF's from the content.  Otherwise, replace the
    # LF's by CR's.  (Otherwise, you would loose line endings that were
    # formed only by LF's.) Write the modified content to the file.
    # (After testing, modify the code so that it writes it back to the same file.)
    f = open(fnameresult, 'wb')
    if content.find('\r') >= 0:
        f.write(content.replace('\n', ''))
        f.write(content.replace('\n', '\r'))
if __name__ == '__main__':

Open in new window
butterhookAuthor Commented:
Thanks pepr for your answer, didn't see it when I accepted the answer!!
No problem ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.