Unix paths that work for any platform in Python?

后端 未结 7 2038
迷失自我
迷失自我 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:17

    Within python, using / will always work. You will need to be aware of the OS convention if you want to execute a command in a subshell

    myprog = "/path/to/my/program"
    os.system([myprog, "-n"])                           # 1
    os.system([myprog, "C:/input/file/to/myprog"])      # 2
    

    Command #1 will probably work as expected.
    Command #2 might not work if myprog is a Windows command and expects to parse its command line arguments to get a Windows file name.

提交回复
热议问题