所以我正在使用Python,对它还是很新的,我需要能够遍历目录中的所有文件夹,所以如果/foo
包含/foo/bar
和/foo/bar/foo
,我想列出所有条目。到目前为止,我已经创建了一个在它自己的文件中工作的类,但是当我尝试import
它时,我得到一个错误声明TypeError: coercing to Unicode: need string or buffer, builtin_function_or_method found
。你知道吗
函数位于文件DirTree
中,因此我通过以下方式导入它:
from DirTree import DirTrav
DirList = DirTrav(dir).returnList()
代码可以在下面找到。你知道吗
import os
class DirTrav:
DList = []
dir = ""
def __init__(self, dirTrav):
self.dir = dirTrav
def dirTree(self, start):
_subFolders = os.listdir(start)
for f in _subFolders:
_newFolder = os.path.join(start, f)
if os.path.isdir( _newFolder):
self.DList.append(_newFolder)
self.dirTree(_newFolder)
def returnList(self):
self.dirTree(dir)
return self.DList
使用
os.walk()
:相关问题 更多 >
编程相关推荐