【发布时间】:2015-09-29 13:07:12
【问题描述】:
如果它们不包含某些文件类型,我将尝试返回所有目录的唯一列表 (set)。如果未找到该文件类型,则将该目录名称添加到列表中以供进一步审核。
下面的函数将查找所有有效文件夹并将其添加到集合中以进行进一步比较。我想将此扩展为仅返回那些不包含out_list 中的文件的目录。这些目录可能包含带有out_list 文件的子目录。如果这是真的,我只想要有效目录的文件夹名称的路径。
# directory = r'w:\workorder'
#
# Example:
# w:\workorder\region1\12345678\hi.pdf
# w:\workorder\region2\23456789\test\bye.pdf
# w:\workorder\region3\34567891\<empty>
# w:\workorder\region4\45678912\Final.doc
#
# Results:
# ['34567891', '45678912']
job_folders = set([]) #set list is unique
out_list = [".pdf", ".ppt", ".txt"]
def get_filepaths(directory):
"""
This function will generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
folder_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for item in os.listdir(directory):
if os.path.isdir(os.path.join(directory, item)):
folderpath = os.path.join(directory, item) # Join the two strings in order to form the full folderpath.
if re.search('^[0-9]', item):
job_folders.add(item[:8])
folder_paths.append(folderpath) # Add it to the list.
return folder_paths
【问题讨论】:
标签: python directory subdirectory os.walk