diff --git a/Doc/library/idle.rst b/Doc/library/idle.rst
index 0bd248c22b1820..273b5830e4293e 100644
--- a/Doc/library/idle.rst
+++ b/Doc/library/idle.rst
@@ -199,7 +199,8 @@ Format Paragraph
Strip trailing whitespace
Remove trailing space and other whitespace characters after the last
non-whitespace character of a line by applying str.rstrip to each line,
- including lines within multiline strings.
+ including lines within multiline strings. Except for Shell windows,
+ remove extra newlines at the end of the file.
.. index::
single: Run script
diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
index c6aa00d0d54b32..5eb77398d95e60 100644
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -3,6 +3,9 @@ Released on 2020-10-05?
======================================
+bpo-38862: 'Strip Trailing Whitespace' on the Format menu removes extra
+newlines at the end of non-shell files.
+
bpo-38636: Fix IDLE Format menu tab toggle and file indent width. These
functions (default shortcuts Alt-T and Alt-U) were mistakenly disabled
in 3.7.5 and 3.8.0.
diff --git a/Lib/idlelib/format.py b/Lib/idlelib/format.py
index 2b0980565734a4..4b57a182c9a49d 100644
--- a/Lib/idlelib/format.py
+++ b/Lib/idlelib/format.py
@@ -408,6 +408,16 @@ def do_rstrip(self, event=None):
if cut < raw:
text.delete('%i.%i' % (cur, cut), '%i.end' % cur)
+ if (text.get('end-2c') == '\n' # File ends with at least 1 newline;
+ and not hasattr(self.editwin, 'interp')): # & is not Shell.
+ # Delete extra user endlines.
+ while (text.index('end-1c') > '1.0' # Stop if file empty.
+ and text.get('end-3c') == '\n'):
+ text.delete('end-3c')
+ # Because tk indexes are slice indexes and never raise,
+ # a file with only newlines will be emptied.
+ # patchcheck.py does the same.
+
undo.undo_block_stop()
diff --git a/Lib/idlelib/help.html b/Lib/idlelib/help.html
index 0754f2453baf66..09dc4c57bcdc0f 100644
--- a/Lib/idlelib/help.html
+++ b/Lib/idlelib/help.html
@@ -4,7 +4,7 @@