python - PyCharm 警告字符串和字节连接,但一切都是字符串

标签 python string pycharm concatenation

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/

相关文章:

java - Pycharm 3.4 无法在 Yosemite 上运行

python - 在 Postgresql 中将 python 序数日期转换为正常日期

python - PyTorch LSTM 输入维度

python - 如何跨多个服务器/ worker 管理 websockets

Python字符串索引和字符比较

带空格的 Java 字符串填充

python - 如何将轮廓转换为标记以进行绘图?

c# 去除字符串中间的零

python - Pycharm 不在 Jupyter Notebook 中显示宽数据框

python - 使用 IDE 工具调试 Airflow 任务?