我正在与一位同事合作,他使用Ubuntu而我使用Windows。我们有一个包含JSON文件的数据集,其中包含一个名为“path”的字段。他的路径看起来像这样:
'C:/Users/krock/Desktop/FIIT/BP/Ubuntu/luadb/etc/luarocks_test/modules/30log/share/lua/5.3/30log.lua'
但是这在Windows上不起作用,我正在尝试做的是
some_string.replace('/', '\\')
但这会导致 JSON 中的字符串看起来像这样:
'C:\\Users\\krock\\Desktop\\FIIT\\BP\\Ubuntu\\luadb\\etc\\luarocks_test\\data_all'
在我的Windows机器上,我无法读取这些路径(的程序),因为它会出现以下错误:
"No such file or directory"
有解决方案吗?
编辑:我尝试使用pathlib中的Path,但我得到了另一个错误:
"TypeError: Object of type WindowsPath is not JSON serializable"
我发现解决方法是使用 str(Path(path_string)),但结果再次是用双引号括起来的路径。