Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unused object while using os.walk

I have the code below to list all the files in directory and subdirectory.

def GetListFiles(rootdir,fileextension):
    import os
    testfiles = []
    for root,subfolders,files in os.walk(rootdir):
        for file in files:
            ext = "." + fileextension
            if (os.path.splitext(file)[-1].lower()==ext) and (file!="AssemblyInfo.cs"):
                testfiles.append(os.path.join(root,file))
    return testfiles

In the line for root,subfolders,files in os.walk(rootdir):, the variable, subfoldersis not used. If I remove it, it is error

"too many values to unpack (expected 2)"

How can I avoid this?

like image 811
SHRI Avatar asked Sep 04 '25 17:09

SHRI


1 Answers

If you want to use a temporary variable, which is not used anywhere, the convention is to use _.

for root, _, files in os.walk(rootdir):

Note: Unless you use this is REPL, you can use _. In REPL, _ holds the result of the last evaluated expression.

like image 131
thefourtheye Avatar answered Sep 07 '25 18:09

thefourtheye