简体   繁体   English

python 中的正向/反向斜线问题

[英]Forward/Backward Slash Issue in python

I am having a problem setting a new string to os.chdir (to change directory).我在为 os.chdir(更改目录)设置新字符串时遇到问题。


fullDir= '//user/Tester/diagnostics/data/'
fullDirString= fullDir+ uniqueName + '/' + folderNameRun
os.chdir(fullDirString)

Error code I get is "FileNotFoundError: [WinError 3] System cannot find the path specified我得到的错误代码是"FileNotFoundError: [WinError 3] System cannot find the path specified

I have tried this to fix that issue我试过这个来解决这个问题


fullDir= '\\user\Tester\diagnostics\data\'
fullDirString= fullDir+ uniqueName + '\' + folderNameRun
os.chdir(fullDirString)

And I have tried this我已经试过了


newfullDirString= fullDirString.replace('\\', r'\')   

However I am out of luck.但是我运气不好。 Any advice?有什么建议吗?

What about using os.path module?使用os.path模块怎么样? It should take care about the path construction depending on the current OS它应该根据当前操作系统注意路径构建

from os import path
fullDir = '//user/Tester/diagnostics/data/'
fullDirString= path.join(path.normpath(fullDir),uniqueName,folderNameRun)
os.chdir(fullDirString)

Edit 1:编辑 1:

Integrated the path.normpath to normalize the initial path, as suggested by @zmike按照@zmike 的建议,集成path.normpath以规范化初始路径

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

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