使用" *"
检查文件夹路径这是我尝试过的。
microsecond
假
这将始终打印False,还有其他方式可以将其打印为true吗? 我知道当我把静态路径,它可以
import os
checkdir = "/lib/modules/*/kernel/drivers/char"
path = os.path.exists(checkIPMI_dir)
print path
我不能放静态,因为我有一些linux操作系统,它使用不同的内核 版本,那里的号码将不一样。
答案 0 :(得分:0)
这可能无法解决您的问题,但前段时间我需要能够通过目录结构递归未知次数来查找文件,所以我写了这个函数:
import os
import fnmatch
def get_files(root, patterns='*', depth=1, yield_folders=False):
"""
Return all of the files matching patterns, in pattern.
Only search to teh specified depth
Arguments:
root - Top level directory for search
patterns - comma separated list of Unix style
wildcards to match NOTE: This is not
a regular expression.
depth - level of subdirectory search, default
1 level down
yield_folders - True folder names should be returned
default False
Returns:
generator of files meeting search criteria for all
patterns in argument patterns
"""
# Determine the depth of the root directory
root_depth = len(root.split(os.sep))
# Figure out what patterns we are matching
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
# Are we including directories in search?
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
# Determine if we've exceeded the depth of the
# search?
cur_depth = len(path.split(os.sep))
if (cur_depth - root_depth) > depth:
break
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
使用此功能,您可以检查文件是否存在以下内容:
checkdir = "/lib/modules/*/kernel/drivers/char"
matches = get_files(checkdir, depth=100, yield_folders=True)
found = True if matches else False
这一切可能都有点过头了,但它应该有效!