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, subfolders
is not used. If I remove it, it is error
"too many values to unpack (expected 2)"
How can I avoid this?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With