summaryrefslogtreecommitdiffstats
path: root/Lib/os.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-05-08 11:10:09 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2010-05-08 11:10:09 (GMT)
commit449c466e7d17292c46c08ef9a458fbdc5ddb7987 (patch)
tree87f02ca27a0f64708d8618012252d7678f6ac44a /Lib/os.py
parent4cda46ab9167ab6c46e1cbb6caea6fbb021605ac (diff)
downloadcpython-449c466e7d17292c46c08ef9a458fbdc5ddb7987.zip
cpython-449c466e7d17292c46c08ef9a458fbdc5ddb7987.tar.gz
cpython-449c466e7d17292c46c08ef9a458fbdc5ddb7987.tar.bz2
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.
Diffstat (limited to 'Lib/os.py')
-rw-r--r--Lib/os.py11
1 files changed, 11 insertions, 0 deletions
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 @@ if name not in ('os2', 'nt'):
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()