2

On my saving a .py file, IDLE (Python 3.9 x64) will add a newline after the code itself. It is setting off my OCD while making me curious. For instance, take this entirely original last line of a program while coding:

print("Hello, world!")

Yet, when saved, it becomes:

print("Hello, world!")
# ghost line!

What is the significance of that extra line—can I turn it off?

niamulbengali
  • 292
  • 1
  • 3
  • 16
  • This does not seems tobe a real problem, is it ? Could you explain why it is a problem in your case ? In any case you could try another IDE / editor if you like. – Malo Nov 27 '20 at 19:30
  • 2
    https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline –  Nov 27 '20 at 19:33
  • You are not seeing a new line, Just space for the next line. Normally every line in a text ends in a "newline" character. Not having the newline character can be problematic with certain scanning methods, or for, say, back to back file copies. – RufusVS Nov 28 '20 at 16:09

2 Answers2

1

It is the best practice of Python to have an extra line at the end of the code. If you explicitly go and delete the last line, then it won't be there. But, my suggestion would be to keep it. It's always better to follow best practices.

1

IDLE has made sure that a file being saved ends with a newline ('\n') since the first commit in 2000. I believe that at one time the Python compiler required that every line, including the last, be complete, ending with a newline. It may be more lax now, under some circumstances, but I am not sure. There may be other things one does with a file that could require the proper ending.

Ghosts do not exist, and neither does the ghost line. A line of 0 characters is not really a line. The cursor at the left margin after the last line is ready to start a new line.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • Thanks! Please give some examples of what you mean by "There may be other things one does with a file that could require the proper ending." – niamulbengali Nov 28 '20 at 19:41