Python | os.fsencode() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.fsencode()
method in Python is used to encode the specified filename to the filesystem encoding with ‘surrogateescape‘ error handler, or ‘strict‘ on Windows;
Syntax: os.fsencode(filename)
Parameter:
filename: A path-like object representing a file system path. A path-like object is either a str or bytes object representing a path.
Return Type: This method returns a bytestring which represents the encoded filename.
Code: use of os.fsencode() method
import os
filename = "/home/user/File.txt"
encode = os.fsencode(filename)
print (encode)
print (os.fsdecode(encode))
|
Output:
Encoded filename: b'/home/user/F\xc3\x8e\xe2\x95\x9a\xc3\x88.txt'
/home/user/FÎ?È.txt
Reference: https://docs.python.org/3/library/os.html#os.fsencode
Last Updated :
20 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...