pythonc++endl

std::endl, is there an equivalent in Python? (return + flush)


In C++, std::endl is used to add a return and flush the string. In Python, there is no need to use this special functionality, except for specific uses.

One of the uses that may justify its use is when the output is redirected to a file, for example:

outfile = open("output_file.txt", 'a')
sys.stdout = outfile
print("Something")

In this case, the file does not show its content unless the file is closed (outfile.close()) or manually flushed (outfile.flush()).

If an equivalent of std::endl from C++ exists in Python, it could both write the line and flush the file.

I doubt that this functionality exists, but I ask this question to be sure.

Thanks :)


Solution

  • os.linesep is the platform-dependent line separator. If you want something shorter, you can give it an alias when you import it, e.g.:

    from os import linesep as endl