[英]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, subfolders
is not used. 在
for root,subfolders,files in os.walk(rootdir):
,未使用变量subfolders
。 If I remove it, it is error 如果我删除它是错误的
"too many values to unpack (expected 2)"
“要解压缩的值太多(预期为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 _
. 注意:除非使用的是REPL,否则可以使用
_
。 In REPL, _
holds the result of the last evaluated expression. 在REPL中,
_
保留最后一个求值表达式的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.