Luciano Valdez
Luciano Valdez

Reputation: 15

Iterating in python with os.walk not returning the expected values

I am new to python and I've been trying to iterate through a big directory with plenty of subdirectories with certain depth.

Until now i got until this code.

for dirpath, subdirs, files in os.walk("//media//rayeus//Datos//Mis  Documentos//Nueva Carpeta//", topdown=True):
   for name in files:
      f = os.path.join(dirpath, name)
      print f

   for name in subdirs:
      j = os.path.join(dirpath, name)
      print j

the idea is use the iterations to make an inventory on an excel file of the structure inside the directory.

The problem is that if i just leave the same path without "Nueva carpeta" it works perfectly... but when i add "Nueva Carpeta" the script runs without errors but doesnt returns anything

Upvotes: 1

Views: 101

Answers (1)

Ricky Wilson
Ricky Wilson

Reputation: 3359

import os

def crawl(*args, **kw):
    '''
    This will yield all files in all subdirs
    '''
    for root, _, files in os.walk(*args, **kw):
        for fname in files:
            yield os.path.join(root, fname)


for fpath in crawl('.', topdown=1):
    print fpath

Upvotes: 1

Related Questions