Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: os.path.realpath() - how to fix unescaped backslashes?

I want to get the path to the currently executed script.I have used
os.path.realpath(__file__), however, it returns a string like D:\My Stuff\Python\my_script.py without proper backslash escaping! How to escape them?

like image 611
Kotauskas Avatar asked Jul 14 '26 20:07

Kotauskas


1 Answers

path = "D:\My Stuff\Python\my_script.py"
escaped_path = path.replace("\\", "\\\\")
print(escaped_path)

Will output

D:\\My Stuff\\Python\\my_script.py
like image 107
eli-bd Avatar answered Jul 18 '26 19:07

eli-bd