-3

How can I list all sub directories and files using Python?

Example:

firstdir
    secondir
       fin.txt,testdir
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • Please provide more coherent *real* code of what you have tried already so readers can understand where your difficulty lies. As is, you are simply asking the community to write code for you. StackOverflow is *not* a code-writing service. – idjaw Mar 19 '17 at 18:46

1 Answers1

0
import os
root = "/your/path"
path = os.path.join(root, "firstdir")
for path, subdirs, files in os.walk(root):
    for name in files:
        print os.path.join(path, name)

Or you can directly replace your root variable in os.walk with firstdir

Here the method walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up.

Marlon Abeykoon
  • 11,927
  • 4
  • 54
  • 75
  • 1
    @MarlonAbeykoon Next time please don't simply provide answers to questions of this quality. The community works hard at ensuring that questions stay on-topic to avoid repeat questions like this that pollute the space for good questions. Furthermore, if you are going to answer something, then please answer it properly. Dumping code without an explanation is not helpful for anyone. Furthermore, this question most definitely already has an answer and can be duplicated as such. – idjaw Mar 19 '17 at 18:52
  • @idjaw yes I understand, I will explain my code. Thanks – Marlon Abeykoon Mar 19 '17 at 18:54
  • Firstly sorry for my simple question and bad English.I understand but I have already search my question in StackOverflow but I can't find. – Alperen Bedir Mar 19 '17 at 19:00