'os.path.join' 不返回 str?



在Python中我写了:

file_name = parse.unquote(os.path.basename(parse.urlparse(src).path))
directory = os.fsencode('./cache_folder')
file_path = os.path.join(directory, str.encode(file_name))
if imghdr.what(file_path).upper() not in ['J']:

但我收到以下警告:

Expected type 'str | PathLike[str] | _ReadableBinary', got 'bytes' instead

为什么它得到bytes?据我所知,os.path.join返回PathLike[str]

我该怎么解决这个问题?

编辑:

CCD_ 4和CCD_ 5将CCD_ 6转换/编码为CCD_。

使用

directory = './cache_folder'
file_path = os.path.join(directory, file_name)

或者您必须将bytes转换/解码为string

directory = os.fsencode('./cache_folder')
file_path = os.path.join(directory, str.encode(file_name))
#file_path = str.decode(file_path)
file_path = file_path.decode()

最新更新