Unix paths that work for any platform in Python?

后端 未结 7 2040
迷失自我
迷失自我 2021-01-04 17:35

Can all paths in a Python program use \"..\" (for the parent directory) and / (for separating path components), and still work whatever the platform?

On one

相关标签:
7条回答
  • 2021-01-04 17:57

    Windows supports / as a path separator. The only incompatibilities between Unix filenames and Windows filenames are:

    • the allowed characters in filenames
    • the special names and
    • case sensitivity

    Windows is more restrictive in the first two accounts (this is, it has more forbidden characters and more special names), while Unix is typically case sensitive. There are some answers here listing exactly what are these characters and names. I'll see if I can find them.

    Now, if your development environment comes with a function to create or manipulate paths, you should use it, it's there for a reason, y'know. Especially given that there are a lot more platforms than Windows and Unix.

    Answering your first question, yes ../dir/file will work, unless they hit some of the above mentioned incompatibilities.

    0 讨论(0)
  • 2021-01-04 17:57

    OS/X and Linux are both Unix compatible, so by definition they use the format you gave at the beginning of the question. Windows allows "/" in addition to "\" so that programs could be interchangeable with Xenix, a Unix variant that Microsoft was trying out a long time ago, and that compatibility has been carried forward to the present. Thus it works too.

    I don't know how many other platforms Python has been ported to, and I can't speak for them.

    0 讨论(0)
  • 2021-01-04 18:04

    It works on Windows, so if you define "whatever the platform" to be Unix and Windows, you're fine.

    On the other hand, Python also runs on VMS, RISC OS, and other odd platforms that use completely different filename conventions. However, it's probable that trying to get your application to run on VMS, blind, is kind of silly anyway - "premature portability is the root of some relatively minor evil"

    I like using the os.path functions anyway because they are good for expressing intent - instead of just a string concatenation, which might be done for any of a million purposes, it reads very explicitly as a path manipulation.

    0 讨论(0)
  • 2021-01-04 18:06

    As others have said, a forward slash will work in all cases, but you're better off creating a list of path segments and os.path.join()-ing them.

    0 讨论(0)
  • 2021-01-04 18:08

    "Almost always safe" is right. All of the platforms you care about probably work ok today and I don't think they will be changing their conventions any time soon.

    However Python is very portable and runs on a lot more than the usual platforms. The reason for the os module is to help smooth things over it a platform does have different requirements.

    Is there a good reason for you to not use the os functions?

    os.pardir is self documenting whereas ".." isn't, and os.pardir might be easier to grep for

    Here is some docs from python 1.6 when Mac was still different for everything

    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 executables

    Programs 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).

    0 讨论(0)
  • 2021-01-04 18:10

    I've never had any problems with using .., although it might be a good idea to convert it to an absolute path using os.path.abspath. Secondly, I would recommend always using os.path.join whereever possible. There are a lot of corner cases (aside from portability issues) in joining paths, and it's good not to have to worry about them. For instance:

    >>> '/foo/bar/' + 'qux'
    '/foo/bar/qux'
    >>> '/foo/bar' + 'qux'
    '/foo/barqux'
    >>> from os.path import join
    >>> join('/foo/bar/', 'qux')
    '/foo/bar/qux'
    >>> join('/foo/bar', 'qux')
    '/foo/bar/qux'
    

    You may run into problems with using .. if you're on some obscure platforms, but I can't name any (Windows, *nix, and OS X all support that notation).

    0 讨论(0)
提交回复
热议问题