Python os.fsdecode()
Python中的os.fsdecode()方法用于从文件系统编码中解码指定文件名,该文件系统编码为“surrogateescape”错误处理程序,在Windows上为“strict”;
语法:os.fsdecode(filename)
参数:
filename:表示已编码文件的类路径对象。类路径对象是表示路径的str或bytes对象。
返回类型:该方法返回一个表示解码文件名的字符串。
示例1
使用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