So if I want to print only the directory name instead of whole base path, is there any tricks to achieve it 🙂
import os
for root, dirs, files in os.walk("/home/lungsang/Desktop/levelpack-UI/content/A0/1 docx-raw"):
print(root)
Here I get the result :
/home/ubuntu/levelpack-UI/content/A0/1 docx-raw
My desired result : only the directory name only.
1 docx-raw
3
Answers
This one worked for me. thnx !
Standard os.path functionality:
Seems too trivial for the stackoverflow I guess. Anyway…
The simplest solution is
print(root.split("/")[-1])