I use python to write scripts for Autodesk Maya. Maya is a cross-platform software and internally use forward slash. If I use os.path.join operation on windows it can result paths like this:

e:/Test\\TemplatePicture.jpg

My idea is that as long as I don't use ms-dos commands easier way to join path parts like this:

pathPart1 = "e:"
pathPart2 = "Test"
pathPart3 = "TemplatePicture.jpg"
path = "s%/s%/s%" % (pathPart1, pathPart2, pathPart3) 

Is there something that makes it a bad idea?

有帮助吗?

解决方案

When you import os, python will create an os.path specific to your platform. On linux its posixpath and on windows its ntpath. When you are working on Maya paths, use posixpath. It will follow linux conventions even on windows. When you need to go native, convert using the realpath for your current system.

import os
import posixpath

maya_path = posixpath.join('a','b','c')
local_path = os.path.realpath(maya_path)

其他提示

I don't see any problems with this.

In fact there is a related question here.

To summarize the discussion within the provided link - you either let python handle file paths or you do it all yourself

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top