Python| os.fsdecode() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.fsdecode()
方法用于从文件系统编码中解码指定的文件名,使用“ surrogateescape ”错误处理程序,或者在 Windows 上使用“ strict ”;
Syntax: os.fsdecode(filename)
Parameter:
filename: A path-like object representing an encoded file. A path-like object is either a str or bytes object representing a path.
Return Type: This method returns a string which represents the decoded filename.
代码: os.fsdecode()方法的使用
# Python program to explain os.fsdecode() method
# importing os module
import os
# Filename encoded to the filesystem encoding
# with 'surrogateescape' error handler
# or 'strict' error handler
filename = b"/home / user / F\xc3\x8e\xe2\x95\x9a\xc3\x88.txt"
# Decode the above filename
# form the filesystem encoding
# with 'surrogateescape' error handler,
# or 'strict' (On Windows)
decode = os.fsdecode(filename)
# Print the decoded filename
print("Decoded filename:", decode)
# To encode a filename to the filesystem
# encoding with 'surrogateescape' error handler
# or 'strict' error handler os.encode() method
# can be used
输出:
Encoded filename: b'/home/user/F\xc3\x8e\xe2\x95\x9a\xc3\x88.txt'
Decoded filename: /home/user/FÎ?È.txt
参考: https://docs。 Python.org/3/library/os.html#os.fsdecode