Python os.path 模块,rsplit() 实例源码

我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用os.path.rsplit()

项目:tefla    作者:openAGI    | 项目源码 | 文件源码
def get_src_path(obj, src_root='tefla', append_base=True):
    """Creates a src path string with line info for use as markdown link.
    """
    path = getsourcefile(obj)
    if not src_root in path:
        # this can happen with e.g.
        # inlinefunc-wrapped functions
        if hasattr(obj, "__module__"):
            path = "%s.%s" % (obj.__module__, obj.__name__)
        else:
            path = obj.__name__
        path = path.replace(".", "/")
    try:
        pre, post = path.rsplit(src_root + "/", 1)
    except:
        pre, post = '', ''

    lineno = get_line_no(obj)
    lineno = "" if lineno is None else "#L{}".format(lineno)

    path = src_root + "/" + post + lineno
    if append_base:
        path = os.path.join(
            'https://github.com/openagi/tefla/blob/master', path)
    return path
项目:OSPTF    作者:xSploited    | 项目源码 | 文件源码
def gen_package_pickled_dic(path, module_name):
    modules_dic={}
    start_path=module_name.replace(".", "/")
    search_path=os.path.dirname(path)
    logging.info("embedding %s ..."%os.path.join(search_path, start_path))
    #TODO: remove comments from python files || compile to .pyc to make payloads lighter
    if os.path.isdir(path):
        for root, dirs, files in os.walk(os.path.join(search_path, start_path)):
            for f in files:
                module_code=""
                with open(os.path.join(root,f),'rb') as fd:
                    module_code=fd.read()
                modprefix = root[len(search_path.rstrip(os.sep))+1:]
                modpath = os.path.join(modprefix,f).replace("\\","/")
                modules_dic[modpath]=module_code
    elif os.path.isfile(path):
        ext=path.rsplit(".",1)[1]
        module_code=""
        with open(path,'rb') as f:
            module_code=f.read()
        cur=""
        for rep in start_path.split("/")[:-1]:
            if not cur+rep+"/__init__.py" in modules_dic:
                modules_dic[rep+"/__init__.py"]=""
            cur+=rep+"/"

        modules_dic[start_path+"."+ext]=module_code
    if not modules_dic:
        raise NameError("path %s not found"%path)
    return modules_dic
项目:pupy    作者:ru-faraon    | 项目源码 | 文件源码
def gen_package_pickled_dic(path, module_name):
    modules_dic={}
    start_path=module_name.replace(".", "/")
    search_path=os.path.dirname(path)
    logging.info("embedding %s ..."%os.path.join(search_path, start_path))
    #TODO: remove comments from python files || compile to .pyc to make payloads lighter
    if os.path.isdir(path):
        for root, dirs, files in os.walk(os.path.join(search_path, start_path)):
            for f in files:
                module_code=""
                with open(os.path.join(root,f),'rb') as fd:
                    module_code=fd.read()
                modprefix = root[len(search_path.rstrip(os.sep))+1:]
                modpath = os.path.join(modprefix,f).replace("\\","/")
                modules_dic[modpath]=module_code
    elif os.path.isfile(path):
        ext=path.rsplit(".",1)[1]
        module_code=""
        with open(path,'rb') as f:
            module_code=f.read()
        cur=""
        for rep in start_path.split("/")[:-1]:
            if not cur+rep+"/__init__.py" in modules_dic:
                modules_dic[rep+"/__init__.py"]=""
            cur+=rep+"/"

        modules_dic[start_path+"."+ext]=module_code
    if not modules_dic:
        raise NameError("path %s not found"%path)
    return modules_dic
项目:pyecore    作者:pyecore    | 项目源码 | 文件源码
def can_resolve(self, uri_path, from_resource=None):
        uri_path = Resource.normalize(uri_path)
        fragment = uri_path.rsplit('#', maxsplit=1)
        if len(fragment) == 2:
            uri_str, fragment = fragment
        else:
            return False
        if uri_str in self.resources:
            return True
        start = from_resource.uri.normalize() if from_resource else '.'
        apath = path.dirname(start)
        uri = URI(path.join(apath, uri_str))
        return uri.normalize() in self.resources
项目:pyecore    作者:pyecore    | 项目源码 | 文件源码
def resolve(self, uri, from_resource=None):
        upath = Resource.normalize(uri)
        uri_str, fragment = upath.rsplit('#', maxsplit=1)
        if uri_str in self.resources:
            return Resource._navigate_from(fragment, self.resources[uri_str])
        start = from_resource.uri.normalize() if from_resource else '.'
        apath = path.dirname(start)
        uri = URI(path.join(apath, uri_str))
        epackage = self.resources[uri.normalize()]
        if isinstance(epackage, Resource):
            epackage = epackage.contents[0]
        return Resource._navigate_from(fragment, epackage)
项目:pyecore    作者:pyecore    | 项目源码 | 文件源码
def split_path(path):
        path = Resource.normalize(path)
        fragment = path.rsplit('#', maxsplit=1)
        if len(fragment) == 2:
            uri, fragment = fragment
        else:
            uri = None
        return uri, fragment
项目:pyecore    作者:pyecore    | 项目源码 | 文件源码
def resolve(path, registry):
        path = Resource.normalize(path)
        uri, fragment = path.rsplit('#', maxsplit=1)
        epackage = registry[uri]
        return Resource._navigate_from(fragment, epackage)
项目:pyecore    作者:pyecore    | 项目源码 | 文件源码
def _is_external(self, path):
        path = self.normalize(path)
        uri, fragment = (path.rsplit('#', maxsplit=1)
                         if '#' in path else (None, path))
        return uri, fragment
项目:bandit-ss    作者:zeroSteiner    | 项目源码 | 文件源码
def namespace_path_split(path):
    '''Split the namespace path into a pair (head, tail).

    Tail will be the last namespace path component and head will
    be everything leading up to that in the path. This is similar to
    os.path.split.

    :param path: (String) A namespace path.
    :returns: (String, String) A tuple where the first component is the base
              path and the second is the last path component.
    '''
    return tuple(path.rsplit('.', 1))