diff --git a/command/build_scripts.py b/command/build_scripts.py
index <HASH>..<HASH> 100644
--- a/command/build_scripts.py
+++ b/command/build_scripts.py
@@ -128,10 +128,9 @@ class build_scripts(Command):
                             "The shebang ({!r}) is not decodable "
                             "from the script encoding ({})"
                             .format(shebang, encoding))
-                    outf = open(outfile, "wb")
-                    outf.write(shebang)
-                    outf.writelines(f.readlines())
-                    outf.close()
+                    with open(outfile, "wb") as outf:
+                        outf.write(shebang)
+                        outf.writelines(f.readlines())
                 if f:
                     f.close()
             else:
Issue #<I>, issue #<I>: port 6ad<I> fix from distutils to packaging

build_scripts command of packaging now handles correctly non-ASCII path (path
to the Python executable). Open and write the script in binary mode, but ensure
that the shebang is decodable from UTF-8 and from the encoding of the script.