2

I am writing a small script that will need to read and write to text files on Windows and Linux and perhaps Mac even. The script will be used by users on all perhaps all of these platforms (Windows for sure) and interchangeably - so a user who wrote to a file X on Windows, may read the file on Linux with the script.

What precautions should I take or how should I implement my code that it is able to handle line endings across various platforms? (reading and writing)

Or this is a non-issue and Python handles everything?

user225312
  • 126,773
  • 69
  • 172
  • 181
  • 1
    Possible duplicate of http://stackoverflow.com/questions/454725/python-get-proper-line-ending – Rod Dec 23 '10 at 19:58

2 Answers2

3

It's a non-issue, Python is smart like that. It handles line endings across platforms very well.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • @A A Python is usually built with universal newline support, but you can build it without that. I'd recommend you add the `U` just to be safe. It's never been a problem for me, though, but all versions of Python might not behave the same – Rafe Kettler Dec 23 '10 at 20:01
  • Ok all right, I found the answer to that. So I should not bother about it at all. That is good news. I searched for this issue and thought there were issues. – user225312 Dec 23 '10 at 20:02
  • @A A nope, fortunately Python works pretty smoothly across platform. They do a good job of abstracting all the OS-specific stuff. – Rafe Kettler Dec 23 '10 at 20:03
1

A non-issue is something you don't have to pay any attention to for the rest of your life. That's not the case here, it's all too easy to mess up line-endings in Python, even.

For platform-independent writing, see here

For p-i reading, see here

Community
  • 1
  • 1
RolfBly
  • 3,612
  • 5
  • 32
  • 46