N
N
noboxer2022-03-09 20:53:28
Python
noboxer, 2022-03-09 20:53:28

How to properly wrap file path?

Hey! I ran into a problem, how to wrap
the path to the file so that the backslash does not escape?
I know it's common to add " r " to the beginning of the path, but how do I
write that inside the json file?
6228e97ae2a2c766534062.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2022-03-09
@noboxer

You need to double the slashes, since they are also required to be escaped in json.

>>> data = {"path": r"C:\Test\123"}
>>> data
{'path': 'C:\\Test\\123'}
>>> print(json.dumps(data))
{"path": "C:\\Test\\123"}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question