1

I would like to change the cwd to a specific folder. The folder name is known; however, the path to it will vary.

I am attempting the following but cannot seem to get what I am looking for:

absolute_path = os.path.abspath(folder_name)
directory_path = os.path.dirname(absolute_path)
os.chdir(directory_path)

This does not do what I'm looking for because it is keeping the original cwd to where the .py file is run from. I've tried adding os.chdir(os.path.expanduser("~")) prior to the first code block; however, it just creates the absolute_path to /home/user/folder_name.

Of course if there is a simple import that I could use, I'll be open to anything.

What would be the correct way to get the paths of all folders with with a specific name?

9
  • How do you get the path to the folder, I'm confused. Commented Jan 8, 2014 at 19:53
  • this question needs more clarification before anyone can answer (I dont even understand what you are trying to do...) are you trying to search the whole system for a folder? Commented Jan 8, 2014 at 19:54
  • 5
    Where can the folder be? What happens if the name is "foo", but there are both "/a/foo" and "/b/foo"? Commented Jan 8, 2014 at 19:55
  • @JoranBeasley Correct. I am looking for the paths of folders with a specific name Commented Jan 8, 2014 at 20:04
  • 1
    Here is some code I found lying around for using Spotlight via PyObjC from a command-line program (one that doesn't already have a run loop, or threads). Commented Jan 8, 2014 at 21:15

2 Answers 2

3
def find_folders(start_path,needle):
   for cwd, folders, files,in os.walk(start_path):
       if needle in folders:
           yield os.path.join(cwd,needle)

for path in find_folders("/","a_folder_named_x"):
    print path

all this is doing is walking down your directory structure from a given start path and finding all occurances of a folder named needle

in the example it is starting at the root folder of the system and looking for a folder named "a_folder_named_x" ... be forwarned this could take a while to run if you need to search the whole system ...

Sign up to request clarification or add additional context in comments.

3 Comments

This also won't work if the directory is not accessible because of permissions (e.g., I may have rx access on ~you/there, and x access on ~you, but not r access on ~you, which means I couldn't find ~you/there from the top even though I could use it once found). Of course that's not a problem with this code, but a fundamental feature of the POSIX-style permissions and filesystems.
Thanks, I recognized the runtime as an issue.
This is mostly important because, on some systems, you have x but not r access to /home or /Users or the equivalent, meaning you won't even find directories inside your own home this way. And the same may be true for things like /mnt or /Volumes or the equivalent, meaning you won't find directories on external drives or shares that you mounted. And so on.
3

You need to understand that abspath accepts a relative pathname (which might just be a filename), and gives you the equivalent absolute (full) pathname. A relative pathname is one that begins in your current directory; no searching is involved, and so it always points to one place (which may or may not exist).

What you actually need is to search down a directory tree, starting at ~ or whatever directory makes sense in your case, until you find a folder with the requested name. That's what @Joran's code does.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.