PyCharm 警告我这一点:
days = os.listdir(os.path.join(os.path.dirname(__file__), src))
day = days[0]
mystring = day.split('.')[0] + ';' + str(entering)
expected type 'bytes', got 'str' instead
但一切对我来说似乎都是str..
无论如何它有效,但警告让我怀疑。任何提示?
编辑
添加更多详细信息:Pycharm 是 Linux 中的版本 2019.3.3。输入的是int,src是来自的路径:
p = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description="")
p.add_argument('-src', dest='source', action='store', default='results', help='source path')
args = p.parse_args()
src = args.source
最佳答案
如果你看 os.listdir
定义 1 ,您可以阅读部分:
If path is of type bytes (directly or indirectly through the PathLike interface), the filenames returned will also be of type bytes; in all other circumstances, they will be of type str.
我认为这是
os.path.join
的间接原因因为它返回实现 PathLike
的对象界面。我认为 PyCharm 假设 os.path.join
将以某种方式返回字节。最简单的方法就是将路径转换为 str
使用 str(os.path.join(os.path.dirname(__file__), src))
(虽然它已经是一个str)
关于python - PyCharm 警告字符串和字节连接,但一切都是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60503408/