7

I have developed an application in python and pyside. I have developed it on linux machine. Now I want to deploy it on windows machine. Here problem is path. In linux forward slash(/) used as separator but windows uses backward slash(\) as separator.

So, on windows all paths not work. There are several paths in application(for stylesheet, images, log etc.)

Its difficult to change all paths as most of paths are hard code like:

 rootPath()+'/static/images/add.png' #rootPath return os.path...

Example:

 colorPickerBtnStyle = 'background:url(' + rootPath() + '/static/images/color_icon.png);background-repeat: no-repeat;background-position:center center;'

Is there any work around for this problem.

anils
  • 1,782
  • 6
  • 19
  • 29
  • 11
    When did Windows stop accepting either "/" or "\" in paths? It's allowed them since DOS. – stark May 26 '12 at 13:08
  • 3
    The example looks like CSS, where you should always use forward slashes even on Windows anyway, even if the referenced file is local: http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx – Thomas May 26 '12 at 13:32
  • It's css used in desktop application using qt/pyside – anils May 26 '12 at 14:00
  • 1
    Is it possible it's not working on Windows because of letter case insensitivity, rather than a slash direction problem? – martineau May 26 '12 at 14:00
  • -1 "all paths not work" is not useful information. I'm finding this really hard to believe. Apart from in the command prompt, you should have no problems in Windows with paths using / alone, or a mixture of / and backslash. @Anil1010, can you supply one example where you had a problem, and what the problem was? For preference, edit your question and include the actual error message and traceback. – John Machin May 27 '12 at 02:13

3 Answers3

15

os.path.join() will use the right kind of slash on the right platform.

Mattie
  • 20,280
  • 7
  • 36
  • 54
Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 2
    Yes. That's what you sign up for when hardcoding things :) – Thomas May 26 '12 at 13:33
  • Problem solved by using find and replace and added function assetPath which returns image path and path build using os.path.join(). Thak you Thomas!!! – anils May 26 '12 at 13:58
  • Do not use hard coded paths, use os.path.join() to create path. – anils May 27 '12 at 08:53
13

use os.sep instead of explicitly writing the slashes.

Zeugma
  • 31,231
  • 9
  • 69
  • 81
user1413824
  • 659
  • 1
  • 8
  • 15
0

Alternatively you can use join:

os.sep.join((dir, file))
misantroop
  • 2,276
  • 1
  • 16
  • 24