10

Possible Duplicate:
how to list all files of a directory in python

How do I list all directory content using python and add it to a list?

Community
  • 1
  • 1
dkea
  • 690
  • 1
  • 9
  • 24

1 Answers1

6

With Python, there so many ways to do this however, this one is the simplest i know.

Please check the comments for the specifics of the output.

from os import listdir
list = listdir("C:\Users\Hello\World\Python Programs")
print list #Outputs the whole list
print len(list) #Checks for the length of the list
print list[26] #Outputs a specific element in the list

Good luck!

dkea
  • 690
  • 1
  • 9
  • 24
  • 2
    Might want to be careful with backslashes in paths as they conflict with string escape characters. – icktoofay Sep 22 '12 at 07:32
  • Therefore, put "r" before the string: `list = listdir(r"C:\Users\Hello\World\Python Programs")` – Lior Sep 22 '12 at 07:35
  • 2
    Umm... answering your own question is encouraged if you figure out the answer in the meantime, but... why are you acting as if you're two people? (e.g., w/ "Good luck!") – Mu Mind Sep 22 '12 at 07:36
  • @MuMind, well, the good luck is just an expression which shouldn't be a matter to how the question is answered. :) if you want, you can edit the answer and remove it. :) – dkea Sep 22 '12 at 09:51