How to only get the last directory from readlink

306 Views Asked by At

I'm doing a project at work were I need to get the version of Java for 100's of servers. I been using readlink, but that gives me full path for the link. I'm trying to figured out a way to only get the last directory using Python.

>>> f = os.system('readlink  /dir/dir/dir/java')
/dir/dir/dir/dir/jdk

I need the output to only be JDK.

1

There are 1 best solutions below

0
On

After some further research I figured it out. Once I got the output from os.readlink I then took the output and got the basename:

>>> f = os.readlink('/dir/dir/dir/java')
>>> java_version = os.path.basename(f)
'java'