Issue #8514: Add os.fsencode() function (Unix only): encode a string to bytes
for use in the file system, environment variables or the command line.
diff --git a/Lib/os.py b/Lib/os.py
index 3e2ee0d..13ab18c 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -504,6 +504,17 @@
         return environb.get(key, default)
     __all__.append("getenvb")
 
+if name != 'nt':
+    def fsencode(value):
+        """Encode value for use in the file system, environment variables
+        or the command line."""
+        if isinstance(value, bytes):
+            return value
+        elif isinstance(value, str):
+            return value.encode(sys.getfilesystemencoding(), 'surrogateescape')
+        else:
+            raise TypeError("expect bytes or str, not %s" % type(value).__name__)
+
 def _exists(name):
     return name in globals()