6

I apologize if this is a question that has already been resolved. I want to get the current directory when running a Python script or within Python. The following will return the full path including the current directory:

os.getcwd()

I can also get the path all the way up to the current directory:

os.path.dirname(os.getcwd())

Using os.path.split will return the same thing as the above, plus the current folder, but then I end up with an object I want:

(thing_I_dont_want, thing_I_want) = os.path.split(os.getcwd())

Is there a way I can get just the thing I want, the current folder, without creating any objects I don't want around? Alternately, is there something I can put in place of the variable thing_I_dont_wantthat will prevent it from being created (e.g. (*, thing_I_want))?

Thanks!

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
trynthink
  • 301
  • 5
  • 13

4 Answers4

9

Like this:

os.path.split(os.getcwd())[1]

Although os.path.split returns a tuple, you don't need to unpack it. You can simply select the item that you need and ignore the one that you don't need.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I don't know why that didn't occur to me before. I knew there was a simple solution. Time for more coffee. – trynthink Jul 07 '13 at 21:08
4

Use os.path.split:

>>> os.path.split(os.getcwd())
('/home/user', 'py')
>>> os.path.split(os.getcwd())[-1]
'py'

help on os.path.split:

>>> print os.path.split.__doc__
Split a pathname.  Returns tuple "(head, tail)" where "tail" is
    everything after the final slash.  Either part may be empty.
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Well yes, that does return the object ('py' in your example) that I'm looking for, but also returns the rest of the path. Edit - adding the [-1] works. – trynthink Jul 07 '13 at 21:06
  • I could be wrong, but I think rigidly specifying the split character like that isn't going to be as robust as using os.path.split, since os.path will behave appropriately across any platform. – trynthink Jul 07 '13 at 21:20
  • @trynthink Ah! I forgot windows uses \, I'll remove that comment. – Ashwini Chaudhary Jul 07 '13 at 21:22
  • @DavidHeffernan But it's a good reference for someone who's sees this answer in future. – Ashwini Chaudhary Jul 07 '13 at 21:25
1

You could try this, though it's not safe (as all the given solutions) if the pathname ends with a / for some reason:

os.path.basename(os.getcwd())
filmor
  • 30,840
  • 6
  • 50
  • 48
0

The standard pythonic way of denoting that "this is a thing I don't want" is to call it _ - as in:

_, thing_I_want = os.path.split(os.getcwd())

Note that this doesn't do anything special. The object is being created inside os.split(), and it's still being returned and given the name _ - but this does make it clear to people reading your code that you don't care about that particular element.

As well as being a signal to other people, most IDEs and code validators will understand that the variable called _ is to be ignored, and they won't do things like warn you about it never being used.

James Polley
  • 7,977
  • 2
  • 29
  • 33