0

Does it exist in python something like 'std::endl' in c++ std? Or, how can I get an end line symbol of the current system?

It seems very important thing because an end line symbol may be different in different OSs.

4
  • 1
    Just use the character \n. Commented Nov 27, 2017 at 8:44
  • std::endl isn't needed to get a platform-specific line terminator even in C++ - \n in text mode will already do that. Commented Nov 27, 2017 at 8:45
  • Hm, Do we not need to use '\r\n' in Unix? Commented Nov 27, 2017 at 8:48
  • Heck no. Definitely don't use '\r\n' on Unix. Commented Nov 27, 2017 at 17:34

1 Answer 1

5

The os module has linesep which is the platform-specific string to end a line. However, quoting the docs:

Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single '\n' instead, on all platforms.

The default Python behaviour for text files and file-like objects is that if your program writes '\n', it will be translated into whatever is appropriate for the local system. So as Mateen Ulhaq wrote, just use '\n'

Sign up to request clarification or add additional context in comments.

3 Comments

Wow.. It's odd to me when you said: "Put the '\n' to the file', but, the interpreter might do another.
Hm, I still can't find a site where said that python might interpret '\n' in a different way for different OSs, like you said above. In fact, it's very very odd because, if you wanna write '\n' to the text file in text write mode, it will be not equal to that you want
It's been around in Python since 2.7, so one of those things that perhaps isn't made clear "because everybody knows it". Summarised in the doco for TextIOBase and TextIOWrapper: docs.python.org/3/library/io.html#io.TextIOBase

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.