Reputation: 47
I want to understand why every time I print out my os.path.basename(root)
from a sub-directory folder, it always shows an empty list at the beginner of the list. Is there any way to avoid this?
My data folder:
dir_1:
-subdir_1:
-img1.jpg
-subdir_2:
-img2.jpg
My code:
for root, dirs, files in os.walk(path):
print(os.path.basename(root))
My output:
['','subdir_1','subdir_2']
My Expected output:
['subdir_1','subdir_2']
Upvotes: 2
Views: 177
Reputation: 2665
The official documentation explains this:
Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').
Upvotes: 4