Python, add trailing slash to directory string, os independently

后端 未结 4 991
长情又很酷
长情又很酷 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:24

    Since you want to connect a directory and a filename, use

    os.path.join(directory, filename)
    

    If you want to get rid of .\..\..\blah\ paths, use

    os.path.join(os.path.normpath(directory), filename)
    

提交回复
热议问题