[Python] Python - How to write a windows path to a json file?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am working together with a colleague and he has Ubuntu while I have windows. We have a dataset of json files which have in them a "path" written. His paths look like this:

    'C:/Users/krock/Desktop/FIIT/BP/Ubuntu/luadb/etc/luarocks_test/modules/30log/share/lua/5.3/30log.lua'


    But this doesn't work on Windows, I was trying to do

    some_string.replace('/', '\\')


    But this results in strings written in json that look like this:

    'C:\\Users\\krock\\Desktop\\FIIT\\BP\\Ubuntu\\luadb\\etc\\luarocks_test\\data_all'


    On my windows machine, I can't read (the program) these paths as it give an error:


    No such file or directory
    ​

    Is there a solution to this?

    EDIT: I tried using Path from pathlib, but I got another error saying:


    TypeError: Object of type WindowsPath is not JSON serializable
    ​

    I found the solution to this is to do str(Path(path_string)), but the result is again the path in double quotes.

    Continue reading...

Compartilhe esta Página