Using Python's os.path, how do I go up one directory?

前端 未结 14 1769
陌清茗
陌清茗 2020-12-07 07:47

I recently upgrade Django from v1.3.1 to v1.4.

In my old settings.py I have

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname( __file_         


        
14条回答
  •  天命终不由人
    2020-12-07 08:29

    If you are using Python 3.4 or newer, a convenient way to move up multiple directories is pathlib:

    from pathlib import Path
    
    full_path = "path/to/directory"
    str(Path(full_path).parents[0])  # "path/to"
    str(Path(full_path).parents[1])  # "path"
    str(Path(full_path).parents[2])  # "."
    

提交回复
热议问题