Elegant way to take basename of directory in Python?

后端 未结 5 1340
傲寒
傲寒 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 02:12

    You should use os.path.join() to add paths together.

    use

    os.path.dirname(os.path.join(output_dir,''))
    

    to extract dirname, while adding a trailing slash if it was omitted.

提交回复
热议问题