gpt4 book ai didi

Python - 类型错误 : Can't mix strings and bytes in path components

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:26 26 4
gpt4 key购买 nike

以下代码:

import os

directory_in_str = 'C:\\Work\\Test\\'
directory = os.fsencode(directory_in_str)

for file in os.listdir(directory):
filename = os.fsdecode(file)
if filename.lower().endswith(".xml"):
with open(os.path.join(directory, filename), 'r') as handle:
for line in handle:
print(line)
else:
continue

给我这个错误:

Traceback (most recent call last):
File "c:\Work\balance_search2.py", line 9, in <module>
with open(os.path.join(directory, filename), 'r') as handle:
File "C:\ProgramData\Anaconda3\lib\ntpath.py", line 114, in join
genericpath._check_arg_types('join', path, *paths)
File "C:\ProgramData\Anaconda3\lib\genericpath.py", line 151, in _check_arg_types
raise TypeError("Can't mix strings and bytes in path components") from None
TypeError: Can't mix strings and bytes in path components

谁能帮我解决这个问题。

最佳答案

只需删除这一行:directory = os.fsencode(directory_in_str)。无需对目录名称进行编码。

关于Python - 类型错误 : Can't mix strings and bytes in path components,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48729364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com