I have fiddled around with this for a while and have prefer to use use read
in combination with rstrip
. Without rstrip("\n")
, Python adds a newline to the end of the string, which in most cases is not very useful.
with open("myfile.txt") as f:
file_content = f.read().rstrip("\n")
print file_content