boaz
boaz

Reputation: 11

Issue with os.walk function

I'm trying the get from user a string and place it in the os.walk function.
This is my code:

def FiletypeNumber():
    Path=Boaz.get()
    Pathw="'"+Path+"'"
    print (Pathw)
    for (dirpath, dirnames, filenames) in walk(Pathw):
        f.extend(filenames)
    for i in range(len(f)):
        t = f[i]
        indexO=t.rindex('.')
        LenF=len(t)
        Ex=(t[-(LenF-indexO):])
        FileTypeList.append(Ex)
        if Ex in Typofiles:
            pass
        else:
            Typofiles.append(Ex)

When I print the variable Pathw, I get the wanted results (for example: 'd:\js').

But when I pass this variable to the walk function, my code doesn't work properly.
Its purpose is to:

  1. Enter a directory.
  2. Print the number and type of files in it.

Upvotes: 1

Views: 116

Answers (1)

Daniel
Daniel

Reputation: 42778

Don't add ' to your path name, such a path does not exist and results in a empty list.

def FiletypeNumber():
    path = Boaz.get()
    print('{!r}'.format(path))
    for (dirpath, dirnames, filenames) in walk(path):
        for filename in filenames:
            _, ext = os.path.splitext(filename)
            FileTypeList.append(ext)
            if ext not in Typofiles:
                Typofiles.append(ext)

Upvotes: 1

Related Questions