User Tools

Site Tools


task_boiler

This is an old revision of the document!


task boiler

Place to cook the tasks. Once they are boiled enough, move them to a separate plate.

Start the first heading with “=====”

find depth

find depth of a file or directory

import os
 
def get_depth(file_or_dir_path):
    abs_path = os.path.abspath(os.path.expanduser(file_or_dir_path))
    depth = os.path.normpath(abs_path).count(os.sep)
    return depth

Examples:

In [2]: 
os.path.abspath(os.path.expanduser('~'))
Out[2]: 
'/home/rajulocal'

In [3]: 
get_depth('~')
Out[3]: 
2

In [4]: 
get_depth('/home/rajulocal')
Out[4]: 
2

In [5]: 
get_depth('/home/rajulocal/')
Out[5]: 
2

In [6]: 
get_depth('/home/rajulocal/foo')
Out[6]: 
3

In [7]: 
get_depth('/')
Out[7]: 
1

In [8]: 
get_depth('/home')
Out[8]: 
1

Using Python 3.10.6, Ipython 8.6.0

tags | get depth of a directory

task_boiler.1683130674.txt.gz · Last modified: 2023/05/03 16:17 by admin