Python os.fsencode() Method
Example
Encode a file path:
#Import os Library
import os
#Encode filename
encode =
os.fsencode("/home/testuser/desktop/newfile.txt")
#Print the
encoded filename
print(encode)
Try it Yourself »
Definition and Usage
The os.fsencode()
method encodes a file path.
This method encodes the filename to the filesystem encoding with 'surrogateescape' error handler, or 'strict' on Windows.
Note: os.fsdecode() is the reverse function.
Syntax
os.fsencode(filename)
Parameter Values
Parameter | Description |
---|---|
filename | Required. The file path to encode |
Technical Details
Return Value: | A bytes value, representing the encoded
path |
---|---|
Python Version: | 3.2 |
Change Log: | 3.6: Support added to accept objects implementing the os.PathLike interface |