7

Hi I have read articles related converting backward to forward slashes. But sol was to use raw string.

But Problem in my case is :

I will get file path dynamically to a variable var='C:\dummy_folder\a.txt' In this case i need to convert it to Forward slashes. But due to '\a',i am not able to convert to forward slashes

How to i convert it? OR How should i change this string to raw string so that i can change it to forward slash

lbz
  • 9,560
  • 2
  • 30
  • 35
abhishek
  • 73
  • 1
  • 1
  • 3
  • 1
    Where does this string "dynamically" come from? The main place backslash has a special meaning in your Python programs is in your Python source code string literals, which should be static. – Mike Graham Nov 28 '10 at 16:54
  • **Why** do you want to convert them? – martineau Nov 28 '10 at 21:57
  • On Windows, Python outputs `C:\path\to\file.txt`; however, I desire to run a command in git bash which should be `/c/path/to/file.txt` – Jake Berger Oct 31 '13 at 07:00

5 Answers5

13

Don't do this. Just use os.path and let it handle everything. You should not explicitly set the forward or backward slashes.

>>> var=r'C:\dummy_folder\a.txt'
>>> var.replace('\\', '/')
'C:/dummy_folder/a.txt'

But again, don't. Just use os.path and be happy!

user225312
  • 126,773
  • 69
  • 172
  • 181
  • thanks for the ans,but ihave tried os.path, but it wont change backward to forward slash because '\a',\n' will be processed before converting – abhishek Nov 28 '10 at 15:29
  • Abhishek, you can replace it using the way I showed above, but you should not . – user225312 Nov 28 '10 at 17:26
  • The poroblem is there are libraries that CRASAH with windows path format (even on windows) and we NEED to convert those. –  Oct 24 '20 at 17:39
12

There is also os.path.normpath(), which converts backslashes and slashes depending on the local OS. Please see here for detailed usage info. You would use it this way:

>>> string = r'C:/dummy_folder/a.txt'
>>> os.path.normpath(string)
'C:\dummy_folder\a.txt'
hopia
  • 4,880
  • 7
  • 32
  • 54
  • 1
    Thanks for pointing out normpath, however, it only works one way: Converting forward slashes to back slashes. It doesn't work the other way (back slash to forward slash). I guess the best way is to assume linux paths and make sure all path strings use forward slashes. Then, call normpath() on the string just before it is used. – hopia Feb 22 '11 at 21:36
  • This is for me the best answer. Under Mac OS X, `os.path.normpath` preserves forward slashes, as expected. Under Windows, slashes are converted to backslashes. – fatuhoku Oct 23 '13 at 22:38
2

Handling paths as a mere string could put you into troubles.; even more if the path you are handling is an user input or may vary in unpredictable ways.

Different OS have different way to express the path of a given file, and every modern programming language has own methods to handle paths and file system references. Surely Python and Ruby have it:

If you really need to handle strings:

  • Python: string.replace
  • Ruby : string.gsub
lbz
  • 9,560
  • 2
  • 30
  • 35
1

Raw strings are for string literals (written directly in the source file), which doesn't seem to be the case here. In any case, forward slashes are not special characters -- they can be embedded in a regular string without problems. It's backslashes that normally have other meaning in a string, and need to be "escaped" so that they get interpreted as literal backslashes.

To replace backslashes with forward slashes:

# Python:
string = r'C:\dummy_folder\a.txt'
string = string.replace('\\', '/')

# Ruby:
string = 'C:\\dummy_folder\\a.txt'
string = string.gsub('\\', '/')
Cameron
  • 96,106
  • 25
  • 196
  • 225
0
>>> 'C:\\dummy_folder\\a.txt'.replace('\\', '/')
'C:/dummy_folder/a.txt'

In a string literal, you need to escape the \ character.

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • Thanks for sol, How did u change single backward slashes to 2 backward slashes? – abhishek Nov 28 '10 at 15:28
  • 1
    You need to code it like that with two backslashes. A backslash is a special character, and to get one true backslash in the string literal you need to type two. – moinudin Nov 28 '10 at 15:36