Elegant way to take basename of directory in Python?

后端 未结 5 1339
傲寒
傲寒 2021-02-20 01:30

I have several scripts that take as input a directory name, and my program creates files in those directories. Sometimes I want to take the basename of a directory given to the

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-20 01:59

    To deal with your "trailing slash" issue (and other issues!), sanitise user input with os.path.normpath().

    To build paths, use os.path.join()

提交回复
热议问题