一方面,我从来没有在文档中看到这样的声明(我可能错过了),而os和os.path模块确实提供了以不可知的平台方式处理路径的设施(os.pardir,os.path).加入…),这让我认为他们在这里是一个理由.
另一方面,您可以通过read on StackOverflow“../path/to/file”在所有平台上运行…
所以,os.pardir,os.path.join和朋友总是使用,为了便于携带,还是Unix路径名永远是安全的(最多可能是字符编码问题)?或者可能“几乎总是”安全(即在Windows,OS X和Linux下工作)?
然而Python是非常便携式的,比常规平台运行的更多. os模块的原因是帮助平台上的平台有不同的要求.
有没有一个很好的理由你不使用os函数?
os.pardir是自我记录,而“..”不是,os.pardir可能更容易grep
以下是一些来自python 1.6的文档,当Mac对于所有内容都是不同的
OS routines for Mac,DOS,NT,or Posix depending on what system we’re
on.This exports:
– all functions from posix,nt,dos,os2,mac,or ce,e.g. unlink,stat,etc.
– os.path is one of the modules posixpath,ntpath,macpath,or dospath
– os.name is ‘posix’,‘nt’,‘dos’,‘os2’,‘mac’,or ‘ce’
– os.curdir is a string representing the current directory (‘.’ or ‘:’)
– os.pardir is a string representing the parent directory (‘..’ or ‘::’)
– os.sep is the (or a most common) pathname separator (‘/’ or ‘:’ or ‘\’)
– os.altsep is the alternate pathname separator (None or ‘/’)
– os.pathsep is the component separator used in $PATH etc
– os.linesep is the line separator in text files (‘ ‘ or ‘ ‘ or ‘ ‘)
– os.defpath is the default search path for executablesPrograms that import and use ‘os’ stand a better chance of being portable between different platforms. Of course,they must then only use functions that are defined by all platforms (e.g.,unlink and opendir),and leave all pathname manipulation to os.path (e.g.,split and join).