Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14215

Does Python still print a trailing newline after printing the contents of a file?

$
0
0

Disclaimer: I know I essentially answer my own question, but please humor me anyway.

I'm working through a Python book I have (Python Crash Course 2nd Edition), and it says, "The only difference between this output and the original file is the extra blank line at the end of the output. The blank line appears because read() returns an empty string when it reaches the end of the file; this empty string shows up as blank line. If you want to remove the extra blank line, you can use rstrip() in the call to print()." That is the exact wording they use in the book. However, when I do what they tell me to do, no blank line is printed.

All of my code:

with open('/workspaces/functions/files_exceptions/pi_digits.txt', encoding='utf-8') as file_object:    contents = file_object.read()print(contents)print("Hello World!") # To show a newline is not printed

Everything in the file I'm reading from:

 3.1415926535      8979323846      2643383279

Output:

3.1415926535  8979323846  2643383279Hello World!

The output the book shows (the underlines are meant to represent a blank line (Stack Overflow won't let me include a blank line as code)):

3.1415926535  8979323846  2643383279____________

As you can see, there is no trailing newline printed by Python. Does it not do this anymore? I know Python is now in version 3.12, so I'm wondering if it's a recent change made to Python. It might not be though, because the VS Code repo I'm working in was created before the update (I don't know whether or not it updates automatically, or if you have to do it manually (and if so, how) (please tell me the answer to this)).


Viewing all articles
Browse latest Browse all 14215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>