How to get only the last part of a path in Python?

后端 未结 9 542
长发绾君心
长发绾君心 2020-11-28 19:01

In Python, suppose I have a path like this:

/folderA/folderB/folderC/folderD/

How can I get just the folderD part?

9条回答
  •  死守一世寂寞
    2020-11-28 19:42

    I like the parts method of Path for this:

    grandparent_directory, parent_directory, filename = Path(export_filename).parts[-3:]
    log.info(f'{t: <30}: {num_rows: >7} Rows exported to {grandparent_directory}/{parent_directory}/{filename}')
    

提交回复
热议问题