bpo-22831: Use "with" to avoid possible fd leaks in tools (part 1). (GH-10926)

diff --git a/Tools/scripts/gprof2html.py b/Tools/scripts/gprof2html.py
index 4ca705c..b14def4 100755
--- a/Tools/scripts/gprof2html.py
+++ b/Tools/scripts/gprof2html.py
@@ -28,14 +28,7 @@
         for line in fp:
             yield html.escape(line)
 
-
-def main():
-    filename = "gprof.out"
-    if sys.argv[1:]:
-        filename = sys.argv[1]
-    outputfilename = filename + ".html"
-    input = add_escapes(filename)
-    output = open(outputfilename, "w")
+def gprof2html(input, output, filename):
     output.write(header % filename)
     for line in input:
         output.write(line)
@@ -78,7 +71,16 @@
                 part = '<a href="#call:%s">%s</a>' % (part, part)
             output.write(part)
     output.write(trailer)
-    output.close()
+
+
+def main():
+    filename = "gprof.out"
+    if sys.argv[1:]:
+        filename = sys.argv[1]
+    outputfilename = filename + ".html"
+    input = add_escapes(filename)
+    with open(outputfilename, "w") as output:
+        gprof2html(input, output, filename)
     webbrowser.open("file:" + os.path.abspath(outputfilename))
 
 if __name__ == '__main__':