Unix paths that work for any platform in Python?

后端 未结 7 2037
迷失自我
迷失自我 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 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.

提交回复
热议问题