这里的简单问题:我正在尝试识别名称中包含特定字符串的文件夹,但我想指定一些其他排除条件。现在,我正在寻找使用以下语法以特定字符串开头的所有文件夹:
import os
parent_cause = 'B03'
path = ('filepath')
child_causes = [x for x in os.listdir(path) if x.startswith(parent_cause + '.')]
虽然这确实标识了我要查找的子文件夹('B03.1','B03.2'),但它还包含我想要排除的更深层子文件夹('B03.1.1','B03.1.2') 。有关识别开始字符串的子文件夹的简单算法的任何想法,但排除包含两个或更多“。”的子文件夹。比父母?
答案 0 :(得分:0)
我确定我完全理解这些问题,但我建议os.walk
good_dirs = []
bad_dirs = []
for root, files, dirs in os.walk("/tmp/folder/B03"):
# this will walk recursively depth first into B03
# root will be the pwd, so we can test for that
if root.count(".") == 1: ###i think aregex here might help
good_dirs.append(root)
else:
bad_dirs.append(root)
答案 1 :(得分:0)
尝试使用正则表达式
import os
import re
parent_cause = 'B03'
path = ('filepath')
validPath = []
for eachDir in os.listdir(path):
if re.match('^%s\.\d+$' % parent_cause, eachDir):
validPath.append(path+'/'+eachDir)