0

I have a directory and I need to make a path list for the sub directories.

For example my main directory is

C:\A

which contains four different sub directories : A1,A2,A3,A4

I need a list like this:

Path_List = ["C:\A\A1","C:\A\A2","C:\A\A3","C:\A\A4"]

Cheers

YOBA
  • 2,759
  • 1
  • 14
  • 29
Mike
  • 111
  • 3
  • If you think of a question that represents what you kind of help you need or where you are stuck, please edit that into the question so we might help you. – Tim Sep 10 '15 at 11:28

2 Answers2

1
import os

base_dir = os.getcwd()
sub_dirs = [os.path.join(base_dir, d) for d in os.listdir(base_dir)]
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
0

You could (and should, i think) use os module, wich is easy to use and provides a lot of stuff to deal with paths.

I wont say anymore, because your question is vague and shows no effort on searching. You are welcome!!

sub_dirs = os.listdir(os.getcwd()) ## Assuming you start the script in C:/A
path_list = []
for i in sub_dirs:
    path_list.append(os.path.join(os.getcwd(),i))

Dirty and fast.

David P.
  • 125
  • 9
  • I have already searched. I have found 2 ways to call sub_directories . One is os.walk and another one is os.listdir( ). But I don't know how to make a list for paths. – Mike Sep 10 '15 at 11:47
  • As explanation. os.getcwd() get Current Working Directory. In your case 'C:/A'. Have care with this, you should be prepared to deal with relative and absolute paths. Oh, and of course i dont thougt about slashes. Care with that too. – David P. Sep 10 '15 at 11:53
  • Many Thanks for ur response – Mike Sep 10 '15 at 12:14
  • I'm a bit surprised you was not drowned in negative points :) In adition, i edit my answer to give you less problems with slashes using os.path.join(). Only difference is that you dont need to worry about what kind of path (or so) are you using. – David P. Sep 10 '15 at 12:42