-1

I actually think I know the answer to this, and it is:

current_working_directory = os.getcwd().split("/")
local_working_directory = current_working_directory[len(current_working_directory)-1]

this works for me. none of the other posts I've checked out (ex:Find current directory and file's directory) seem to explain how to get the local directory, as opposed to the whole directory path. so posting this as an already answered question. perhaps the question should be: how do I post the answer to a question I've already answered, in order to help others out? and hey, perhaps there's a better answer :-)

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 3
    If only there were [an api for that](http://docs.python.org/2/library/os.path.html#os.path.basename)... – vanza Nov 14 '13 at 19:14
  • See [this](http://meta.stackexchange.com/a/17467/189583) meta post. Users with 100+ rep can answer their own questions. Because you have less, you'll have to endure an eight hour waiting period. If you want to know how to do this in Fortran too, that's fine, but please ask that in a separate post. – Kevin Nov 14 '13 at 19:22

3 Answers3

2

I would use basename

import os

path = os.getcwd()
print(os.path.basename(path))
MxLDevs
  • 19,048
  • 36
  • 123
  • 194
  • that doesn't actually work. that gives me the directory just above the one I'm currently in. ? – juggler Nov 14 '13 at 19:22
  • Works on my machine. When I run this while I'm in "C:\users\kevin\projects", It prints "projects". That's what you're asking for, right? – Kevin Nov 14 '13 at 19:29
  • my understanding is that if my directories were the same as yours, my machine would print out: "kevin".. I'm running.. using windows to interface with a super-computer.. not sure what system the super-computer uses. interesting. keep in mind that I have a solution that works, so this is more of a curiosity now. hopefully others will benefit from your answer :-) – juggler Nov 14 '13 at 20:01
  • Are you using `dirname` or `basename`? – MxLDevs Nov 14 '13 at 20:03
  • I used basename when I tried your code.. – juggler Nov 14 '13 at 20:08
  • comment in wrong place, sorry. – juggler Nov 14 '13 at 20:17
1

Try these

import os

print("Path at terminal when executing this file")
print(os.getcwd() + "\n")

print("This file path, relative to os.getcwd()")
print(__file__ + "\n")

print("This file full path (following symlinks)")
full_path = os.path.realpath(__file__)
print(full_path + "\n")

print("This file directory and name")
path, file = os.path.split(full_path)
print(path + ' --> ' + file + "\n")

print("This file directory only")
print(os.path.dirname(full_path))

Taken from here: Find current directory and file's directory

Edit: here is another from that question

current_folder_name = os.path.split(os.getcwd())
Community
  • 1
  • 1
Funkyguy
  • 628
  • 2
  • 10
  • 31
  • current_folder_name = os.path.split(os.getcwd()) gives me an error: current folder name: /home/mikethe/previous_best_results/s4b Traceback (most recent call last): File "/home/mikethe/bin/curve_fit.py", line 24, in print 'current folder name: ' + current_folder_name1 TypeError: cannot concatenate 'str' and 'tuple' objects – juggler Nov 14 '13 at 20:20
0

os.path contains lots of useful path manipulation functions. I think you're looking for os.path.basename. It is preferable to use os.path because your program will be cross-platform: currently, your solution would not work for Windows. The cross-platform way of getting the name of the directory you're in would be

import os
cwd = os.getcwd()

# use os.path.basename instead of your own function!
print(os.path.basename(cwd))

# Evaluates to True if you have Unix-y path separators: try it out!
os.path.basename(cwd) == cwd.split('/')[-1] 
>>> True
Cody Piersall
  • 8,312
  • 2
  • 43
  • 57
  • interesting. so clearly it isn't the windows I'm interfacing from that is producing my output. see my comment above. – juggler Nov 14 '13 at 20:07