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.

voltento
  • 833
  • 10
  • 26

1 Answers1

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'

user2357112
  • 260,549
  • 28
  • 431
  • 505
Hugh Fisher
  • 2,321
  • 13
  • 8
  • Wow.. It's odd to me when you said: "Put the '\n' to the file', but, the interpreter might do another. – voltento Nov 28 '17 at 06:30
  • 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 – voltento Nov 28 '17 at 06:39
  • 1
    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: https://docs.python.org/3/library/io.html#io.TextIOBase – Hugh Fisher Nov 28 '17 at 08:35