81

I am trying to get the code to list all the directories in a folder, change directory into that folder and get the name of the current folder. The code I have so far is below and isn't working at the minute. I seem to be getting the parent folder name.

import os

for directories in os.listdir(os.getcwd()): 
    dir = os.path.join('/home/user/workspace', directories)
    os.chdir(dir)
    current = os.path.dirname(dir)
    new = str(current).split("-")[0]
    print new

I also have other files in the folder but I do not want to list them. I have tried the below code but I haven't got it working yet either.

for directories in os.path.isdir(os.listdir(os.getcwd())): 

Can anyone see where I am going wrong?

Thanks

Got it working but it seems a bit round about.

import os
os.chdir('/home/user/workspace')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
    dir = os.path.join('/home/user/workspace', dirs)
    os.chdir(dir)
    current = os.getcwd()
    new = str(current).split("/")[4]
    print new
ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
chrisg
  • 40,337
  • 38
  • 86
  • 107

6 Answers6

106

This will print all the subdirectories of the current directory:

print [name for name in os.listdir(".") if os.path.isdir(name)]

I'm not sure what you're doing with split("-"), but perhaps this code will help you find a solution?

If you want the full pathnames of the directories, use abspath:

print [os.path.abspath(name) for name in os.listdir(".") if os.path.isdir(name)]

Note that these pieces of code will only get the immediate subdirectories. If you want sub-sub-directories and so on, you should use walk as others have suggested.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • @RichieHindle -- +1 but I have already tried this. It gets me into the directories but I can't seem to get the name of each directory that I am in. It seems to give me the current path to that directory – chrisg Apr 22 '10 at 12:01
  • 1
    @chrissygormley: I'm not sure what your code is doing, but to get from a full pathname to a single directory name, you can say `dirname = os.path.split(pathname)[1]` – RichieHindle Apr 22 '10 at 12:14
  • 12
    In case of searching in a different directory: `print [name for name in os.listdir(MAIN_DIR) if os.path.isdir(os.path.join(MAIN_DIR, name))]` – Deena Aug 09 '16 at 14:32
27

For New Versions of Python

I liked the answer of @RichieHindle but I add a small fix for it

import os

folder = './my_folder'

sub_folders = [name for name in os.listdir(folder) if os.path.isdir(os.path.join(folder, name))]

print(sub_folders)

otherwise it's not really work for me

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
26
import os
for root, dirs, files in os.walk(top, topdown=False):
    for name in dirs:
        print os.path.join(root, name)

Walk is a good built-in for what you are doing

corn3lius
  • 4,857
  • 2
  • 31
  • 36
9

You seem to be using Python as if it were the shell. Whenever I've needed to do something like what you're doing, I've used os.walk()

For example, as explained here: [x[0] for x in os.walk(directory)] should give you all of the subdirectories, recursively.

gary
  • 4,227
  • 3
  • 31
  • 58
meatvest
  • 879
  • 6
  • 11
4

Listing the entries in the current directory (for directories in os.listdir(os.getcwd()):) and then interpreting those entries as subdirectories of an entirely different directory (dir = os.path.join('/home/user/workspace', directories)) is one thing that looks fishy.

ndim
  • 35,870
  • 12
  • 47
  • 57
0

Slight correction for python3 (same answer as @RichieHindle)

This will print all the subdirectories of the current directory in an array:

print( [name for name in os.listdir(".") if os.path.isdir(name)] )

To make the above simpler to read

for name in os.listdir("."):
    if os.path.isdir(name):
        print(name)

If you want the full pathnames of the directories, use abspath:

print( [os.path.abspath(name) for name in os.listdir(".") if os.path.isdir(name)])

Note that these pieces of code will only get the immediate subdirectories.

Kishan K
  • 671
  • 7
  • 18