How to fix slash issue in windows path in python?

I developed an application in python and pyside. I developed it on a Linux machine. Now I want to deploy it on a Windows machine. Here the problem is the way. On linux, forward slash (/) is used as a delimiter, but windows use a backslash (\) as a delimiter.

So, on windows all the paths do not work. There are several ways in the application (for style sheets, images, magazines, etc.).

It's hard to change all the paths, since most of the paths are hard code, for example:

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 for this problem.

+6
source share
3 answers

os.path.join() will use the correct slash view on the right platform.

+13
source

use os.sep instead of explicitly writing slashes.

+12
source

Alternatively you can use join:

 os.sep.join((dir, file)) 
0
source

Source: https://habr.com/ru/post/916743/


All Articles