有没有与python等价的Cos.path.normpath?

2024-10-03 19:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中,我获取一个原始字符串,然后将其传递给操作系统路径.normpath使它成为一个正确的路径名。我需要的东西都逃出来了,我不用担心。有没有C的等价物?在

这是一个python代码片段。在

myClassProjectPath = os.path.normpath(r"C:\Users\Documents\SharpDevelop Projects\Project With Spaces\MyClass\bin\Debug")

编辑:我担心斜杠和空格。现在,我正在代码中硬编码路径,但很快就会成为用户输入,所以我无法控制“/”vs“\”


Tags: path字符串代码路径projectoswithusers
3条回答

另请查看:

string normalizedPath = Path.GetDirectoryName(path);

这也将修复/vs\。在

您可以始终在前面添加@来创建逐字字符串文本。在

What's the @ in front of a string in C#?

尝试Path.Combine没有任何内容的路径,或者Path.ChangeExtension它到Path.GetExtension。也许这些无用的程序中的一个(或另一个)正常化了。在

除此之外:Pathdoesn’t似乎包含函数。在

或者,你可以自己编程。来自Python文档:

Normalize a pathname. This collapses redundant separators and up-level references
so that A//B, A/B/, A/./B and A/foo/../B all become A/B.

It does not normalize the case (use normcase() for that). On Windows, it converts
forward slashes to backward slashes. It should be understood that this may change
the meaning of the path if it contains symbolic links!

相关问题 更多 >