简体   繁体   English

行尾以及对文本文件的读写

[英]Line endings and reading and writing to text files

I am writing a small script that will need to read and write to text files on Windows and Linux and perhaps Mac even. 我正在编写一个小脚本,需要在Windows和Linux甚至Mac上读写文本文件。 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. 用户可能会在所有这些平台(肯定是Windows)上使用该脚本,并且可以互换使用-因此,在Windows上写入文件X的用户可以使用该脚本在Linux上读取该文件。

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? 还是这不是问题,Python可以处理所有事情?

It's a non-issue, Python is smart like that. 这不是问题,Python很聪明。 It handles line endings across platforms very well. 它很好地处理了跨平台的行尾。

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. 事实并非如此,甚至很容易弄乱Python中的行尾。

For platform-independent writing , see here 有关平台无关的编写 ,请参见此处

For pi reading , see here 有关pi 阅读 ,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM