Scott
Scott

Reputation: 13

Delimit os.walk to look only files and not directories

I have a little question, I have a directorie which contain directories (with files in it) and files. Can i use os.walk to treat files 1 by 1 but not the files into directories ?

Thank you for your answers

Upvotes: 1

Views: 880

Answers (4)

Scott
Scott

Reputation: 13

My code with os.listdir: enter image description here

I think there is something easier for both but i don't know what :/

Upvotes: 0

Scott
Scott

Reputation: 13

Well for exemple i have a directorie like that:

boite_noire/ .....helloworld/ .....test1.txt .....test2.txt

I would like somethink like that at the end of the script: boite_noire/ .....helloworld/ .....test1/ .....test2/

And in the test1 dirctorie I put test1.txt and same for test2.

I tried listdir but without success and yes os.walk.next()2 should be a good idea cause my problem is when i run my script my os.walk scan the directories and files inside and i don't want to, i only want him to scan the files at the source.

My code with os.walk enter image description here

Upvotes: 0

Colwin
Colwin

Reputation: 2685

Do you want to only list the files in the highest level dir without going in to sub-dirs? os.listdir should do it for you.

You can easily add a check to skip dirs this way

 for f in os.listdir(path):
     if f.is_dir():
         continue
     print f

Upvotes: 1

itzMEonTV
itzMEonTV

Reputation: 20339

What about

os.walk("/path/to/dir").next()[2]

Upvotes: 0

Related Questions