Always close files in distutils code and tests (#10252).
diff --git a/Lib/distutils/file_util.py b/Lib/distutils/file_util.py
index 65aa7e0..c36e712 100644
--- a/Lib/distutils/file_util.py
+++ b/Lib/distutils/file_util.py
@@ -234,6 +234,8 @@
     sequence of strings without line terminators) to it.
     """
     f = open(filename, "w")
-    for line in contents:
-        f.write(line + "\n")
-    f.close()
+    try:
+        for line in contents:
+            f.write(line + "\n")
+    finally:
+        f.close()