Python: how to check if a subpath path

Suppose I have the following paths:

/tmp/a
/tmp/abc
/tmp/abc/d/my_file.py

How can I check if a /tmp/abc/d/my_file.pysubpath is /tmp/abc? I tried:

file_path.startswith(dir_path)

But it returns Truefor the directory /tmp/a, my_file.pynot in it.

+4
source share
1 answer

try the following: file_path.startswith(os.path.abspath(dir_path)+os.sep)

You can also check this: How to check if a directory is a secondary directory of another directory

+4
source

Source: https://habr.com/ru/post/1524118/


All Articles