0

checking folder path with "*"

here is what i have tried.

import os

    checkdir = "/lib/modules/*/kernel/drivers/char"
    path = os.path.exists(checkIPMI_dir)
    print path

False

this will always print False, is there other way that i can print it to true? i know when i put static path, it can

import os

    checkdir = "/lib/modules"
    path = os.path.exists(checkIPMI_dir)
    print path

i cant put static, because i have a few linux OS, that uses different kernel version, there for the number will not be the same.

Ajwad Ibrahim
  • 127
  • 1
  • 5
  • 11
  • See http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python – Selcuk May 13 '16 at 04:01
  • The primitive file system calls are not going to look for special characters and attempt to do pattern matching on them (which would possibly turn a single string into multiple matched paths or no path at all). You need to expand your patterns *before* calling low-level things like `os.path.exists`. If an asterisk appears in the strings they're given, then they will literally look for an asterisk in the file name, just as you'd expect. – Tom Karzes May 13 '16 at 04:05
  • Maybe have the path set on each machine/setup via an environment variable? And use that in your code. – davo36 May 13 '16 at 04:27
  • Possible duplicate of [Use wildcard with os.path.isfile()](http://stackoverflow.com/questions/4296138/use-wildcard-with-os-path-isfile) – tripleee May 13 '16 at 05:12

1 Answers1

0

This may not solve your problem, but a while ago I needed to be able to recurse an unknown number of times through a directory structure to find a file, so I wrote this function:

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

With this function you could check to see if a file exists with the following:

checkdir = "/lib/modules/*/kernel/drivers/char"
matches = get_files(checkdir, depth=100, yield_folders=True)
found = True if matches else False

All this may be overkill, but it should work!

aquil.abdullah
  • 3,059
  • 3
  • 21
  • 40