Python os.path.split() Examples

The following are 30 code examples for showing how to use os.path.split(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module os.path , or try the search function .

Example 1
Project: tartarus   Author: sergiooramas   File: load_w2v.py    License: MIT License 6 votes vote down vote up
def load_data_set(index):
    id2file = dict()
    f=open(index_file)
    for line in f.readlines():
        id, text_file = line.strip().split("\t")
        id2file[id] = text_file

    texts = []
    all_sentences = []
    for item in index:
        file = TEXT_DIR+id2file[item]
        text = open(file).read()
        sentences = text.split("\n")

        clean_sentences = [clean_str(s).split(" ") for s in sentences]
        all_sentences.extend(clean_sentences)

        clean_words = [word for s in clean_sentences for word in s]
        texts.append(clean_words)

    return texts, all_sentences 
Example 2
Project: md2notion   Author: Cobertos   File: upload.py    License: MIT License 6 votes vote down vote up
def filesFromPathsUrls(paths):
    """
    Takes paths or URLs and yields file (path, fileName, file) tuples for 
    them
    """
    for path in paths:
        if '://' in path:
            r = requests.get(path)
            if not r.status_code < 300: #TODO: Make this better..., should only accept success
                raise RuntimeError(f'Could not get file {path}, HTTP {r.status_code}')
            fileName = path.split('?')[0]
            fileName = fileName.split('/')[-1]
            fileLike = io.StringIO(r.text)
            fileLike.name = path
            yield (path, fileName, fileLike)
        else:
            globPaths = glob.glob(path, recursive=True)
            if not globPaths:
                raise RuntimeError(f'No file found for glob {path}')
            for path in globPaths:
                with open(path, "r", encoding="utf-8") as file:
                    yield (path, os.path.basename(path), file) 
Example 3
Project: seamseg   Author: mapillary   File: dataset.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, root_dir, split_name, transform):
        super(ISSDataset, self).__init__()
        self.root_dir = root_dir
        self.split_name = split_name
        self.transform = transform

        # Folders
        self._img_dir = path.join(root_dir, ISSDataset._IMG_DIR)
        self._msk_dir = path.join(root_dir, ISSDataset._MSK_DIR)
        self._lst_dir = path.join(root_dir, ISSDataset._LST_DIR)
        for d in self._img_dir, self._msk_dir, self._lst_dir:
            if not path.isdir(d):
                raise IOError("Dataset sub-folder {} does not exist".format(d))

        # Load meta-data and split
        self._meta, self._images = self._load_split() 
Example 4
Project: seamseg   Author: mapillary   File: dataset.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, in_dir, transform):
        super(ISSTestDataset, self).__init__()
        self.in_dir = in_dir
        self.transform = transform

        # Find all images
        self._images = []
        for img_path in chain(
                *(glob.iglob(path.join(self.in_dir, '**', ext), recursive=True) for ext in ISSTestDataset._EXTENSIONS)):
            _, name_with_ext = path.split(img_path)
            idx, _ = path.splitext(name_with_ext)

            with Image.open(img_path) as img_raw:
                size = (img_raw.size[1], img_raw.size[0])

            self._images.append({
                "idx": idx,
                "path": img_path,
                "size": size,
            }) 
Example 5
Project: seamseg   Author: mapillary   File: test_panoptic.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def save_prediction_raw(raw_pred, _, img_info, out_dir):
    # Prepare folders and paths
    folder, img_name = path.split(img_info["rel_path"])
    img_name, _ = path.splitext(img_name)
    out_dir = path.join(out_dir, folder)
    ensure_dir(out_dir)
    out_path = path.join(out_dir, img_name + ".pth.tar")

    out_data = {
        "sem_pred": raw_pred[0],
        "bbx_pred": raw_pred[1],
        "cls_pred": raw_pred[2],
        "obj_pred": raw_pred[3],
        "msk_pred": raw_pred[4]
    }
    torch.save(out_data, out_path) 
Example 6
Project: moler   Author: nokia   File: cmds_events_doc.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _walk_moler_python_files(path, *args):
    """
    Walk thru directory with commands and search for python source code (except __init__.py)
    Yield relative filepath to parameter path

    :param path: relative path do directory with commands
    :type path:
    :rtype: str
    """
    repo_path = abspath(join(path, '..', '..'))

    observer = "event" if "events" in split(path) else "command"
    print("Processing {}s test from path: '{}'".format(observer, path))

    for (dirpath, _, filenames) in walk(path):
        for filename in filenames:
            if filename.endswith('__init__.py'):
                continue
            if filename.endswith('.py'):
                abs_path = join(dirpath, filename)
                in_moler_path = relpath(abs_path, repo_path)
                yield in_moler_path 
Example 7
Project: moler   Author: nokia   File: cmds_events_doc.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _walk_moler_commands(path, base_class):
    for fname in _walk_moler_python_files(path=path):
        pkg_name = fname.replace(".py", "")
        parts = pkg_name.split(sep)
        pkg_name = ".".join(parts)
        moler_module = import_module(pkg_name)
        for _, cls in moler_module.__dict__.items():
            if not isinstance(cls, type):
                continue
            if not issubclass(cls, base_class):
                continue
            module_of_class = cls.__dict__['__module__']
            # take only Commands
            # take only the ones defined in given file (not imported ones)
            if (cls != base_class) and (module_of_class == pkg_name):
                yield moler_module, cls 
Example 8
Project: recruit   Author: Frank-qlu   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def run_script(self, script_name, namespace):
        script = 'scripts/' + script_name
        if not self.has_metadata(script):
            raise ResolutionError("No script named %r" % script_name)
        script_text = self.get_metadata(script).replace('\r\n', '\n')
        script_text = script_text.replace('\r', '\n')
        script_filename = self._fn(self.egg_info, script)
        namespace['__file__'] = script_filename
        if os.path.exists(script_filename):
            source = open(script_filename).read()
            code = compile(source, script_filename, 'exec')
            exec(code, namespace, namespace)
        else:
            from linecache import cache
            cache[script_filename] = (
                len(script_text), 0, script_text.split('\n'), script_filename
            )
            script_code = compile(script_text, script_filename, 'exec')
            exec(script_code, namespace, namespace) 
Example 9
Project: recruit   Author: Frank-qlu   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def _index(self):
        try:
            return self._dirindex
        except AttributeError:
            ind = {}
            for path in self.zipinfo:
                parts = path.split(os.sep)
                while parts:
                    parent = os.sep.join(parts[:-1])
                    if parent in ind:
                        ind[parent].append(parts[-1])
                        break
                    else:
                        ind[parent] = [parts.pop()]
            self._dirindex = ind
            return ind 
Example 10
Project: recruit   Author: Frank-qlu   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def _by_version_descending(names):
    """
    Given a list of filenames, return them in descending order
    by version number.

    >>> names = 'bar', 'foo', 'Python-2.7.10.egg', 'Python-2.7.2.egg'
    >>> _by_version_descending(names)
    ['Python-2.7.10.egg', 'Python-2.7.2.egg', 'foo', 'bar']
    >>> names = 'Setuptools-1.2.3b1.egg', 'Setuptools-1.2.3.egg'
    >>> _by_version_descending(names)
    ['Setuptools-1.2.3.egg', 'Setuptools-1.2.3b1.egg']
    >>> names = 'Setuptools-1.2.3b1.egg', 'Setuptools-1.2.3.post1.egg'
    >>> _by_version_descending(names)
    ['Setuptools-1.2.3.post1.egg', 'Setuptools-1.2.3b1.egg']
    """
    def _by_version(name):
        """
        Parse each component of the filename
        """
        name, ext = os.path.splitext(name)
        parts = itertools.chain(name.split('-'), [ext])
        return [packaging.version.parse(part) for part in parts]

    return sorted(names, key=_by_version, reverse=True) 
Example 11
Project: recruit   Author: Frank-qlu   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def parse(cls, src, dist=None):
        """Parse a single entry point from string `src`

        Entry point syntax follows the form::

            name = some.module:some.attr [extra1, extra2]

        The entry name and module name are required, but the ``:attrs`` and
        ``[extras]`` parts are optional
        """
        m = cls.pattern.match(src)
        if not m:
            msg = "EntryPoint must be in 'name=module:attrs [extras]' format"
            raise ValueError(msg, src)
        res = m.groupdict()
        extras = cls._parse_extras(res['extras'])
        attrs = res['attr'].split('.') if res['attr'] else ()
        return cls(res['name'], res['module'], attrs, extras, dist) 
Example 12
Project: recruit   Author: Frank-qlu   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def _dep_map(self):
        try:
            return self.__dep_map
        except AttributeError:
            dm = self.__dep_map = {None: []}
            for name in 'requires.txt', 'depends.txt':
                for extra, reqs in split_sections(self._get_metadata(name)):
                    if extra:
                        if ':' in extra:
                            extra, marker = extra.split(':', 1)
                            if invalid_marker(marker):
                                # XXX warn
                                reqs = []
                            elif not evaluate_marker(marker):
                                reqs = []
                        extra = safe_extra(extra) or None
                    dm.setdefault(extra, []).extend(parse_requirements(reqs))
            return dm 
Example 13
Project: diabetic-retinopathy-code   Author: hoytak   File: create_image_sframes.py    License: MIT License 6 votes vote down vote up
def load_perturbed_image(d):

    in_file = d['path']
    seed = "%d-%d" % (random_seed, d['row_number'])

    out_file = (out_file_base + in_file).replace(".jpeg", "-perturbed-%s.jpeg" % seed)

    base_dir = split(out_file)[0]
    
    if not os.path.exists(base_dir):
        os.makedirs(base_dir)

    if not os.path.exists(out_file):
        try:
            subprocess.check_call('./make_perturbed_image.sh %s %s %s' % (in_file, out_file, seed), shell=True)
        except Exception:
            pass

    return attempt_image_load(in_file, out_file) 
Example 14
Project: adversarial-policies   Author: HumanCompatibleAI   File: highest_win_rate.py    License: MIT License 6 votes vote down vote up
def main():
    logging.basicConfig(level=logging.INFO)
    parsed_args = get_args()
    output_path = parsed_args.output_path
    # If no output path is given, default to saving it in the first logdir under a fixed name
    if output_path is None:
        if len(parsed_args.logdir) > 1:
            raise ValueError("Must specify --output_path when using multiple log directories.")
        output_path = os.path.join(parsed_args.logdir[0], 'highest_win_policies_and_rates.json')

    for logdir in parsed_args.logdir:
        if 'multi_train' not in logdir.split(os.path.sep):
            logger.warning(f"logdir '{logdir}' does not contain 'multi_train'."
                           "Falling back to absolute paths, JSON may not be portable.")

    logger.info(f"Output path: {output_path}")
    logger.info(f"Log dir: {parsed_args.logdir}")
    with open(output_path, 'w') as f:  # fail fast if output_path inaccessible
        result = find_best(parsed_args.logdir, parsed_args.episode_window)
        json.dump(result, f) 
Example 15
Project: jbox   Author: jpush   File: __init__.py    License: MIT License 6 votes vote down vote up
def run_script(self, script_name, namespace):
        script = 'scripts/'+script_name
        if not self.has_metadata(script):
            raise ResolutionError("No script named %r" % script_name)
        script_text = self.get_metadata(script).replace('\r\n', '\n')
        script_text = script_text.replace('\r', '\n')
        script_filename = self._fn(self.egg_info, script)
        namespace['__file__'] = script_filename
        if os.path.exists(script_filename):
            source = open(script_filename).read()
            code = compile(source, script_filename, 'exec')
            exec(code, namespace, namespace)
        else:
            from linecache import cache
            cache[script_filename] = (
                len(script_text), 0, script_text.split('\n'), script_filename
            )
            script_code = compile(script_text, script_filename,'exec')
            exec(script_code, namespace, namespace) 
Example 16
Project: jbox   Author: jpush   File: __init__.py    License: MIT License 6 votes vote down vote up
def _index(self):
        try:
            return self._dirindex
        except AttributeError:
            ind = {}
            for path in self.zipinfo:
                parts = path.split(os.sep)
                while parts:
                    parent = os.sep.join(parts[:-1])
                    if parent in ind:
                        ind[parent].append(parts[-1])
                        break
                    else:
                        ind[parent] = [parts.pop()]
            self._dirindex = ind
            return ind 
Example 17
Project: jbox   Author: jpush   File: __init__.py    License: MIT License 6 votes vote down vote up
def _rebuild_mod_path(orig_path, package_name, module):
    """
    Rebuild module.__path__ ensuring that all entries are ordered
    corresponding to their sys.path order
    """
    sys_path = [_normalize_cached(p) for p in sys.path]
    def position_in_sys_path(path):
        """
        Return the ordinal of the path based on its position in sys.path
        """
        path_parts = path.split(os.sep)
        module_parts = package_name.count('.') + 1
        parts = path_parts[:-module_parts]
        return sys_path.index(_normalize_cached(os.sep.join(parts)))

    orig_path.sort(key=position_in_sys_path)
    module.__path__[:] = [_normalize_cached(p) for p in orig_path] 
Example 18
Project: jbox   Author: jpush   File: __init__.py    License: MIT License 6 votes vote down vote up
def _dep_map(self):
        try:
            return self.__dep_map
        except AttributeError:
            dm = self.__dep_map = {None: []}
            for name in 'requires.txt', 'depends.txt':
                for extra, reqs in split_sections(self._get_metadata(name)):
                    if extra:
                        if ':' in extra:
                            extra, marker = extra.split(':', 1)
                            if invalid_marker(marker):
                                # XXX warn
                                reqs=[]
                            elif not evaluate_marker(marker):
                                reqs=[]
                        extra = safe_extra(extra) or None
                    dm.setdefault(extra,[]).extend(parse_requirements(reqs))
            return dm 
Example 19
Project: jbox   Author: jpush   File: __init__.py    License: MIT License 6 votes vote down vote up
def _index(self):
        try:
            return self._dirindex
        except AttributeError:
            ind = {}
            for path in self.zipinfo:
                parts = path.split(os.sep)
                while parts:
                    parent = os.sep.join(parts[:-1])
                    if parent in ind:
                        ind[parent].append(parts[-1])
                        break
                    else:
                        ind[parent] = [parts.pop()]
            self._dirindex = ind
            return ind 
Example 20
Project: jbox   Author: jpush   File: __init__.py    License: MIT License 6 votes vote down vote up
def parse(cls, src, dist=None):
        """Parse a single entry point from string `src`

        Entry point syntax follows the form::

            name = some.module:some.attr [extra1, extra2]

        The entry name and module name are required, but the ``:attrs`` and
        ``[extras]`` parts are optional
        """
        m = cls.pattern.match(src)
        if not m:
            msg = "EntryPoint must be in 'name=module:attrs [extras]' format"
            raise ValueError(msg, src)
        res = m.groupdict()
        extras = cls._parse_extras(res['extras'])
        attrs = res['attr'].split('.') if res['attr'] else ()
        return cls(res['name'], res['module'], attrs, extras, dist) 
Example 21
Project: jbox   Author: jpush   File: __init__.py    License: MIT License 6 votes vote down vote up
def _dep_map(self):
        try:
            return self.__dep_map
        except AttributeError:
            dm = self.__dep_map = {None: []}
            for name in 'requires.txt', 'depends.txt':
                for extra, reqs in split_sections(self._get_metadata(name)):
                    if extra:
                        if ':' in extra:
                            extra, marker = extra.split(':', 1)
                            if invalid_marker(marker):
                                # XXX warn
                                reqs=[]
                            elif not evaluate_marker(marker):
                                reqs=[]
                        extra = safe_extra(extra) or None
                    dm.setdefault(extra,[]).extend(parse_requirements(reqs))
            return dm 
Example 22
Project: vsphere-storage-for-docker   Author: vmware-archive   File: vmdk_utils.py    License: Apache License 2.0 6 votes vote down vote up
def get_vm_config_path(vm_name):
    """Returns vm_uuid for given vm_name, or None """
    si = vmdk_ops.get_si()
    try:
        vm = FindChild(GetVmFolder(), vm_name)
        config_path = vm.summary.config.vmPathName
    except:
        return None

    # config path has the format like this "[datastore1] test_vm1/test_vm1/test_vm1.vmx"
    datastore, path = config_path.split()
    datastore = datastore[1:-1]
    datastore_path = os.path.join("/vmfs/volumes/", datastore)
    # datastore_path has the format like this /vmfs/volumes/datastore_name
    vm_config_path = os.path.join(datastore_path, path)
    return vm_config_path 
Example 23
Project: sanic   Author: huge-success   File: response.py    License: MIT License 5 votes vote down vote up
def file(
    location,
    status=200,
    mime_type=None,
    headers=None,
    filename=None,
    _range=None,
):
    """Return a response object with file data.

    :param location: Location of file on system.
    :param mime_type: Specific mime_type.
    :param headers: Custom Headers.
    :param filename: Override filename.
    :param _range:
    """
    headers = headers or {}
    if filename:
        headers.setdefault(
            "Content-Disposition", f'attachment; filename="{filename}"'
        )
    filename = filename or path.split(location)[-1]

    async with await open_async(location, mode="rb") as f:
        if _range:
            await f.seek(_range.start)
            out_stream = await f.read(_range.size)
            headers[
                "Content-Range"
            ] = f"bytes {_range.start}-{_range.end}/{_range.total}"
            status = 206
        else:
            out_stream = await f.read()

    mime_type = mime_type or guess_type(filename)[0] or "text/plain"
    return HTTPResponse(
        body=out_stream,
        status=status,
        headers=headers,
        content_type=mime_type,
    ) 
Example 24
Project: landmarkerio-server   Author: menpo   File: landmark.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def lm_ids(self, asset_id):
        r"""
        Return
        """
        lm_files = self._lm_paths(asset_id=asset_id)
        return [p.splitext(p.split(f)[-1])[0] for f in lm_files] 
Example 25
Project: landmarkerio-server   Author: menpo   File: landmark.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def asset_id_to_lm_id(self):
        r"""
        Return a dict mapping asset ID's to landmark IDs that are
        present on this server for that asset.
        """
        lm_files = self._lm_paths()
        mapping = defaultdict(list)
        for lm_path in lm_files:
            dir_path, filename = p.split(lm_path)
            lm_id = p.splitext(filename)[0]
            asset_id = p.split(dir_path)[1]
            mapping[asset_id].append(lm_id)
        return mapping 
Example 26
Project: qutebrowser   Author: qutebrowser   File: standarddir.py    License: GNU General Public License v3.0 5 votes vote down vote up
def _writable_location(typ: QStandardPaths.StandardLocation) -> str:
    """Wrapper around QStandardPaths.writableLocation.

    Arguments:
        typ: A QStandardPaths::StandardLocation member.
    """
    typ_str = debug.qenum_key(QStandardPaths, typ)

    # Types we are sure we handle correctly below.
    assert typ in [
        QStandardPaths.ConfigLocation, QStandardPaths.DataLocation,
        QStandardPaths.CacheLocation, QStandardPaths.DownloadLocation,
        QStandardPaths.RuntimeLocation, QStandardPaths.TempLocation,
        # FIXME old Qt
        getattr(QStandardPaths, 'AppDataLocation', object())], typ_str

    with _unset_organization():
        path = QStandardPaths.writableLocation(typ)

    log.misc.debug("writable location for {}: {}".format(typ_str, path))
    if not path:
        raise EmptyValueError("QStandardPaths returned an empty value!")

    # Qt seems to use '/' as path separator even on Windows...
    path = path.replace('/', os.sep)

    # Add the application name to the given path if needed.
    # This is in order for this to work without a QApplication (and thus
    # QStandardsPaths not knowing the application name).
    if (typ != QStandardPaths.DownloadLocation and
            path.split(os.sep)[-1] != APPNAME):
        path = os.path.join(path, APPNAME)

    return path 
Example 27
Project: hyperledger-py   Author: yeasy   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def match_path(path, pattern):
    pattern = pattern.rstrip('/')
    pattern_components = pattern.split('/')
    path_components = path.split('/')[:len(pattern_components)]
    return fnmatch('/'.join(path_components), pattern) 
Example 28
Project: hyperledger-py   Author: yeasy   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def convert_tmpfs_mounts(tmpfs):
    if isinstance(tmpfs, dict):
        return tmpfs

    if not isinstance(tmpfs, list):
        raise ValueError(
            'Expected tmpfs value to be either a list or a dict, found: {}'
            .format(type(tmpfs).__name__)
        )

    result = {}
    for mount in tmpfs:
        if isinstance(mount, six.string_types):
            if ":" in mount:
                name, options = mount.split(":", 1)
            else:
                name = mount
                options = ""

        else:
            raise ValueError(
                "Expected item in tmpfs list to be a string, found: {}"
                .format(type(mount).__name__)
            )

        result[name] = options
    return result 
Example 29
Project: hyperledger-py   Author: yeasy   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def split_command(command):
    if six.PY2 and not isinstance(command, six.binary_type):
        command = command.encode('utf-8')
    return shlex.split(command) 
Example 30
Project: pysat   Author: pysat   File: supermag_magnetometer.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def init(self):
    """Initializes the Instrument object with instrument specific values.

    Runs once upon instantiation.

    Parameters
    ----------
    self : pysat.Instrument
        This object

    Returns
    --------
    Void : (NoneType)
        Object modified in place.


    """

    # if the tag is 'indices', update data_path to reflect this
    # both 'indices' and 'all' are stored under 'all'
    if self.tag == "indices":
        psplit = path.split(self.files.data_path[:-1])
        self.files.data_path = path.join(psplit[0], "all", "")

    # reset the list_remote_files routine to include the data path
    # now conveniently included with instrument object
    self._list_remote_rtn = \
        functools.partial(list_remote_files,
                          data_path=self.files.data_path,
                          format_str=self.files.file_format)
    return