简体   繁体   English

如何修复python中windows上的路径中的斜杠问题?

[英]How to fix forward slash issue in path on windows in python?

I have developed an application in python and pyside. 我在python和pyside中开发了一个应用程序。 I have developed it on linux machine. 我在linux机器上开发了它。 Now I want to deploy it on windows machine. 现在我想在Windows机器上部署它。 Here problem is path. 这里的问题是路径。 In linux forward slash(/) used as separator but windows uses backward slash(\\) as separator. 在linux正斜杠(/)用作分隔符但Windows使用反斜杠(\\)作为分隔符。

So, on windows all paths not work. 因此,在Windows上所有路径都不起作用。 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. 有没有解决这个问题的方法。

os.path.join()将在正确的平台上使用正确的斜杠。

使用os.sep而不是显式写斜杠。

或者你可以使用join:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM