Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find parent directory of a path

Is there any way to find the parent directory of a path using NSFileManager or something?

e.g. Take this:

/path/to/something

And turn it into

/path/to/

like image 443
indragie Avatar asked Aug 21 '09 00:08

indragie


People also ask

What is the parent folder of a file?

A folder that is one level up from the current directory in a file hierarchy.

How do I get parent directory in terminal?

The .. means “the parent directory” of your current directory, so you can use cd .. to go back (or up) one directory. cd ~ (the tilde). The ~ means the home directory, so this command will always change back to your home directory (the default directory in which the Terminal opens).

How do I go to parent directory in Python?

Get the Parent Directory in Python Using the path. parent() Method of the pathlib Module. The path. parent() method, as the name suggests, returns the parent directory of the given path passed as an argument in the form of a string.


1 Answers

The NSString method -stringByDeletingLastPathComponent does just that.

You can use it like this:

NSLog(@"%@", [@"/tmp/afolder" stringByDeletingLastPathComponent]); 

And it will log /tmp.

like image 108
Jon Hess Avatar answered Sep 18 '22 13:09

Jon Hess