行结束以及对文本文件的读取和写入
我正在编写一个小脚本,需要在 Windows 和 Linux 甚至 Mac 上读取和写入文本文件。该脚本将被所有或许所有这些平台(当然是 Windows)上的用户使用并且可以互换 - 因此在 Windows 上写入文件 X
的用户可以使用该脚本在 Linux 上读取该文件。
我应该采取哪些预防措施,或者应该如何实现我的代码,使其能够跨不同平台处理行结尾? (阅读和写作)
或者这不是问题,Python 可以处理一切?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是问题,Python 就是这么聪明。它可以很好地处理跨平台的行结尾。
It's a non-issue, Python is smart like that. It handles line endings across platforms very well.
非问题是指你余生都不需要关注的事情。这里的情况并非如此,甚至在 Python 中很容易弄乱行结束符。
对于独立于平台的编写,请参阅此处
pi 阅读,请参阅此处
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