1

Basically what I need to do: I've got a folder with folders inside and folders inside. For example:

some folder(level0)
    deeper folder(level1)
        deeper folder(level2)
            some_txt(level3)
            some_txt2(level3)
    deeper folder(level1)
        deeper folder(level2)
            some_txt(level3)
            some_txt2(level3)
    deeper folder(level1)
        deeper folder(level2)
            some_txt(level3)
            some_txt2(level3)

I need to do some actions on all text files, but I've got no idea how to do recursion on folders to find those files. Probably need to use something like if type is file {do something}, and if nope, just look forward. Any ideas how to start?

Biffen
  • 6,249
  • 6
  • 28
  • 36
degath
  • 1,530
  • 4
  • 31
  • 60
  • Please share your code and error if any. Please check this link - http://stackoverflow.com/help/how-to-ask – Dinesh Pundkar Aug 22 '16 at 10:02
  • I'd recommend you to use [os.walk](https://docs.python.org/2/library/os.html#os.walk) – BPL Aug 22 '16 at 10:03
  • Adding my code wont do anything, because I've got problems on really early stage of coding. I dont know how to "begin" thats my problem. – degath Aug 22 '16 at 10:04
  • 1
    The down-votes are because you clearly didn't spend any time on this problem before posting. A five-second Google search would have at least pointed you to `os.walk`. – Jonathon Reinhart Aug 22 '16 at 10:04
  • I agree with @JonathonReinhart `os.walk` works fine here! – GIZ Aug 22 '16 at 10:07
  • @JonathonReinhart I wouldnt agree with you, I was spending a time to find it, but I'm new here and also in programming, so sometimes even if I look at right solution I'm not sure it will help me out. Ofc this down-votes are lessons for me to practice more, but really sometimes I've got a blocker and I think its better to ask instead of wasting 10 or more hours. Anyway, Thanks for advice, best regards, Degath :) – degath Aug 25 '16 at 07:54

1 Answers1

1

Use os.walk.

import os
for root, dirs, files in os.walk('some folder'):
    for name in files:
        print('found file', name, 'in directory', r)
xmcp
  • 3,347
  • 2
  • 23
  • 36