task_boiler
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
task_boiler [2023/05/03 16:17] – admin | task_boiler [2024/01/23 22:55] (current) – raju | ||
---|---|---|---|
Line 4: | Line 4: | ||
Start the first heading with " | 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]: | ||
- | '/ | ||
- | |||
- | In [3]: | ||
- | get_depth(' | ||
- | Out[3]: | ||
- | 2 | ||
- | |||
- | In [4]: | ||
- | get_depth('/ | ||
- | Out[4]: | ||
- | 2 | ||
- | |||
- | In [5]: | ||
- | get_depth('/ | ||
- | Out[5]: | ||
- | 2 | ||
- | |||
- | In [6]: | ||
- | get_depth('/ | ||
- | Out[6]: | ||
- | 3 | ||
- | |||
- | In [7]: | ||
- | get_depth('/' | ||
- | Out[7]: | ||
- | 1 | ||
- | |||
- | In [8]: | ||
- | get_depth('/ | ||
- | 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