Unix paths that work for any platform in Python?

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

提交回复
热议问题