How do I take a directory name from a given dir?

Michael Ekstrand michael at elehack.net
Mon May 1 15:57:48 EDT 2006


On Mon, May 01, 2006 at 12:42:58PM -0700, Merrigan wrote:
> The issue I am currently having isto "extract" the directory name from
> a given directory string. For example: from the string
> "/home/testuser/projects/" I need to extract the "projects" part. The
> problem is that the directory names that needs to be used will never be
> the same lenght, so as far as my (very limited) knowledge stretches,
> slicing and indicing is out of the question.

Look at the os.path module, especially the basename and dirname
functions.  basename will extract the "base name", that is, the last
component of a path.

- Michael

-- 
mouse, n: a device for pointing at the xterm in which you want to type.
                -- Fortune
Visit me on the Web: http://www.elehack.net



More information about the Python-list mailing list