rasonage
rasonage

Reputation: 21

Python: change "\" to "\\"

I have an entry field that allows the user to enter their own directory/path structure when saving a file, if the user just copies their directory from windows explorer you can get text like "c:\directory\file.ext" how can I take text entered like that and replace it with the necessary "\" for the os module to use?

Upvotes: 1

Views: 168

Answers (2)

Vinicius Coque
Vinicius Coque

Reputation: 31

Use a raw string to get the backslashes

>>> path = r"c:\test\123"
>>> path
'c:\\test\\123'
>>> print path
c:\test\123

Upvotes: 1

jlarsch
jlarsch

Reputation: 2307

maybe os.path.normpath() would be helpful to you - and the documentation here

Upvotes: 0

Related Questions