get script directory name - Python

spen123 picture spen123 · Jul 7, 2015 · Viewed 64.7k times · Source

I know I can use this to get the full file path

os.path.dirname(os.path.realpath(__file__))

But I want just the name of the folder, my scrip is in. SO if I have my_script.py and it is located at

/home/user/test/my_script.py

I want to return "test" How could I do this?

Thanks

Answer

Joe T. Boka picture Joe T. Boka · Jul 7, 2015
>>> import os
>>> os.getcwd()