Python, add trailing slash to directory string, os independently

后端 未结 4 905
长情又很酷
长情又很酷 2021-01-30 10:02

How can I add a trailing slash (/ for *nix, \\ for win32) to a directory string, if the tailing slash is not already there? Thanks!

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 10:27

    os.path.join(path, '') will add the trailing slash if it's not already there.

    You can do os.path.join(path, '', '') or os.path.join(path_with_a_trailing_slash, '') and you will still only get one trailing slash.

提交回复
热议问题