7

I want to convert a unix file path, which is in forward-slash format, to a windows file path, which is in backward-slash format. I tried both os.path.join() and os.path.normpath() but both of them seems to add double backwards slash to the result. For example, if I use os.path.normpath('static/css/reset.css'), the result is 'static\\css\\reset.css' instead of static\css\reset.css. And 'static/css/reset.css'.replace('/','\\') gives me the same result as os.path.normpath. Is there any way to just get a single-backward-slash-delimited string format?

By the way, I'm using Python2.7 on 64-bit Windows 7.

chaonextdoor
  • 5,019
  • 15
  • 44
  • 61
  • I'm sure you meant to do `'static/css/reset.css'.replace('/','\')` but that's not the ideal solution, I think; you'd want to take the `os.path` route. I don't know the answer, I'm sure someone else will. – 2rs2ts Jul 03 '13 at 00:46
  • 1
    Why can't you just leave the forward slashes alone since they work ok on windows anyway? – John La Rooy Jul 03 '13 at 00:56
  • +1 to that - backslashes are a huge pain for this very reason, and forward slashes work fine for .Net – theodox Jul 03 '13 at 01:41
  • Cute story that might help with the confusion: https://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/ – Michael Scheper Nov 16 '15 at 02:20

3 Answers3

8

'static\\css\\reset.css' is the representation of the string r'static\css\reset.css'.

The double backsalsh indicates escaping of the backslash - in string literals it has a meaning of "do something special with the next character", which you don't want here.

>>> print('static\\css\\reset.css')
static\css\reset.css
Elazar
  • 20,415
  • 4
  • 46
  • 67
  • 2
    The problem is, if I use `'static\\css\\reset.css'` to search the file on my machine, it always shows me `The system cannot find the path specified.` Pretty frustrated:( – chaonextdoor Jul 03 '13 at 00:54
  • @chaonextdoor: does the version with forward slashes work? It looks like your issues might lie elsewhere if that lookup is failing. – Marius Jul 03 '13 at 01:10
  • use normpath to use the systems separator: Unix based will replace th path with '/', and windows, with '\' – Paul Melero Aug 09 '18 at 14:00
4

I'm going to expand on on Elzar's correct answer and your comment. What might have gotten you confused and is left unsaid in the previous answers is that there is a difference between the Python string, as you provide it in the source code, and as the python console and IDEs show it to you, the developer, and the way the string is printed, i.e. displayed to the user:

>>> s = 'c:\\directory\\file.txt'
>>> s
'c:\\directory\\file.txt'  <-- if you ask for the value, you will see double slashes
>>> print s
c:\directory\file.txt      <-- if you print the string, the user will see single slashes

I think the reason you you're getting "The system cannot find the path specified." is because you manually copy to clipboard and paste something from the Python console/IDE (also supported by the fact that your path is shown in quotes in your question).

To add to the confusion, you will sometimes get away with single quotes. Only some slash-character combinations have a special meaning (see Python documentation), e.g. '\n' for a new line and others don't, e.g. '\s', which just prints as \s.

As a side note, the reason for escaped characters is that they are a convenient way for Python/the computer in general to communicate to the programmer what special characters there are in the text. This way, for example, there's no ambiguity between '\t' (tab character) and ' ' (a number of spaces), unprintable/some control characters can actually be seen, etc.

Jan Benes
  • 532
  • 1
  • 5
  • 16
1

static\\css\\reset.css is being shown because "\\" represents "\". If you use this filepath it will be interpreted as 'static\css\reset.css'.

as a check in the interactive shell

>>> list('static\\css\\reset.css')

gives:

['s', 't', 'a', 't', 'i', 'c', '\\', 'c', 's', 's', '\\', 'r', 'e', 's', 'e', 't', '.', 'c', 's', 's']

"\\" will be shown as a single character.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
mbdavis
  • 3,861
  • 2
  • 22
  • 42