当前位置: 首页>>代码示例>>Python>>正文


Python os.PathLike方法代码示例

本文整理汇总了Python中os.PathLike方法的典型用法代码示例。如果您正苦于以下问题:Python os.PathLike方法的具体用法?Python os.PathLike怎么用?Python os.PathLike使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os的用法示例。


在下文中一共展示了os.PathLike方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: compute_hashes

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def compute_hashes(file: "PathLike[str]") -> Dict[str, str]:
    """Compute hashes for a given file/directory.

    :param file_: path-like object pointing to a file/directory.

    :returns: dictionary that contains hash types as keys and corresponding
        hashes as values. There is one entry in this dictionary for each hash
        type in StreamHasher.KNOWN_HASH_TYPES.
        If file_ points to a directory values are empty strings.
    """
    path = Path(file)
    if path.is_dir():
        return {hash_type: "" for hash_type in StreamHasher.KNOWN_HASH_TYPES}

    hasher = StreamHasher()
    with path.open("rb") as stream:
        hasher.compute(stream)
    return {
        hash_type: hasher.hexdigest(hash_type)
        for hash_type in StreamHasher.KNOWN_HASH_TYPES
    } 
开发者ID:genialis,项目名称:resolwe,代码行数:23,代码来源:hasher.py

示例2: validate_url

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def validate_url(wrapped, instance, args, kwargs):
    """Enforces argument named "url" to be relative path.

    Check that it is instance of str or os.PathLike and that it represents
    relative path.
    """
    try:
        # Use -1 since self is not included in the args.
        url = args[getfullargspec(wrapped).args.index("url") - 1]
    except IndexError:
        url = kwargs.get("url")
    if not isinstance(url, (str, PathLike)):
        raise TypeError("Argument 'url' must be a string or path-like object")
    if PurePath(url).is_absolute():
        raise ValueError("Argument 'url' must be a relative path")
    return wrapped(*args, **kwargs) 
开发者ID:genialis,项目名称:resolwe,代码行数:18,代码来源:baseconnector.py

示例3: validate_urls

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def validate_urls(wrapped, instance, args, kwargs):
    """Enforces argument named "urls" to be a list of relative paths."""
    try:
        # Use -1 since self is not included in the args.
        urls = args[getfullargspec(wrapped).args.index("urls") - 1]
    except IndexError:
        urls = kwargs.get("urls")
    # Check that URLS is really a list of strings.
    if not isinstance(urls, list):
        raise TypeError("Argument urls must be a list of strings or path-like objects")
    if not all(isinstance(url, (str, PathLike)) for url in urls):
        raise TypeError("Argument urls must be a list of strings or path-like objects")
    # Check that all URLS are relative.
    if any(PurePath(url).is_absolute() for url in urls):
        raise ValueError("Paths must be relative.")
    return wrapped(*args, *kwargs) 
开发者ID:genialis,项目名称:resolwe,代码行数:18,代码来源:baseconnector.py

示例4: presigned_url

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def presigned_url(
        self,
        url: Union[str, PathLike],
        expiration: int = 10,
        force_download: bool = False,
    ) -> Optional[str]:
        """Create a presigned URL.

        The URL is used to obtain temporary access to the object ar the
        given URL using only returned URL.

        :param expiration: expiration time of the link (in seconds), default
            is 10 seconds.

        :param force_download: force download.

        :returns: URL that can be used to access object or None.
        """
        raise NotImplementedError 
开发者ID:genialis,项目名称:resolwe,代码行数:21,代码来源:baseconnector.py

示例5: uncompress_file

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def uncompress_file(filepath: str or os.PathLike, outpath='.'):
    """
    Unzip a file to the same location of filepath
    uses decompressing algorithm by file extension

    Args:
        filepath (str): path to file
        outpath (str): path to extract to
    """
    filepath = str(filepath)
    if filepath.endswith('.zip'):
        with zipfile.ZipFile(filepath) as z:
            z.extractall(outpath)
    elif filepath.endswith('.gz'):
        if os.path.isdir(outpath):
            raise ValueError('output path for gzip must be a file')
        with gzip.open(filepath, 'rb') as fp:
            file_content = fp.read()
        with open(outpath, 'wb') as fp:
            fp.write(file_content)
    else:
        raise ValueError('Unsupported archive provided. Method supports only .zip/.gz files.') 
开发者ID:shibing624,项目名称:dialogbot,代码行数:24,代码来源:io.py

示例6: write_meta_header

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def write_meta_header(filename: PathLike, meta_dict: Dict[str, Any]):
    """
    Write the MHD meta header file

    :param filename: file to write
    :param meta_dict: dictionary of meta data in MetaImage format
    """
    header = ''
    # do not use tags = meta_dict.keys() because the order of tags matters
    tags = ['ObjectType', 'NDims', 'BinaryData',
            'BinaryDataByteOrderMSB', 'CompressedData', 'CompressedDataSize',
            'TransformMatrix', 'Offset', 'CenterOfRotation',
            'AnatomicalOrientation', 'ElementSpacing',
            'DimSize', 'ElementNumberOfChannels', 'ElementType', 'ElementDataFile',
            'Comment', 'SeriesDescription', 'AcquisitionDate',
            'AcquisitionTime', 'StudyDate', 'StudyTime']
    for tag in tags:
        if tag in meta_dict.keys():
            header += '%s = %s\n' % (tag, meta_dict[tag])
    with open(filename, 'w') as f:
        f.write(header) 
开发者ID:yanlend,项目名称:mhd_utils,代码行数:23,代码来源:__init__.py

示例7: _update_timestamp

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def _update_timestamp(path: os.PathLike, set_new: bool) -> None:
    """
    Context manager to set the timestamp of the path to plus or
    minus a fixed delta, regardless of modifications within the context.

    if set_new is True, the delta is added. Otherwise, the delta is subtracted.
    """
    stats = os.stat(path)
    if set_new:
        new_timestamp = (stats.st_atime_ns + _TIMESTAMP_DELTA, stats.st_mtime_ns + _TIMESTAMP_DELTA)
    else:
        new_timestamp = (stats.st_atime_ns - _TIMESTAMP_DELTA, stats.st_mtime_ns - _TIMESTAMP_DELTA)
    try:
        yield
    finally:
        os.utime(path, ns=new_timestamp)


# Public Methods 
开发者ID:Eloston,项目名称:ungoogled-chromium,代码行数:21,代码来源:domain_substitution.py

示例8: __init__

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def __init__(
        self,
        path: typing.Union[bytes, str, os.PathLike],
        pre_load: bool = None,
        fps: int = None,
        *_,
        **__,
    ):
        assert os.path.isfile(path), f"video [{path}] not existed"
        self.path: str = str(path)
        self.data: typing.Optional[typing.Tuple[VideoFrame]] = tuple()

        self.fps: int = fps
        if fps:
            video_path = os.path.join(tempfile.mkdtemp(), f"tmp_{fps}.mp4")
            logger.debug(f"convert video, and bind path to {video_path}")
            toolbox.fps_convert(fps, self.path, video_path, constants.FFMPEG)
            self.path = video_path

        with toolbox.video_capture(self.path) as cap:
            self.frame_count = toolbox.get_frame_count(cap)
            self.frame_size = toolbox.get_frame_size(cap)

        if pre_load:
            self.load_frames() 
开发者ID:williamfzc,项目名称:stagesepx,代码行数:27,代码来源:video.py

示例9: clean_file

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def clean_file(file):
	"""Returns a tuple containing a ``file``-like object and a close indicator.

	This ensures the given file is opened and keeps track of files that should
	be closed after use (files that were not open prior to this function call).

	Raises
	------
	OSError : Accessing the given file path failed

	Parameters
	----------
	file : Union[str, bytes, os.PathLike, io.IOBase, int]
		A filepath or ``file``-like object that may or may not need to be
		opened
	"""
	if isinstance(file, int):
		return os.fdopen(file, 'rb', closefd=False), True
	elif not hasattr(file, 'read'):
		return open(convert_path(file), 'rb'), True
	else:
		return file, False 
开发者ID:ipfs-shipyard,项目名称:py-ipfs-http-client,代码行数:24,代码来源:utils.py

示例10: clean_files

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def clean_files(files):
	"""Generates tuples with a ``file``-like object and a close indicator.

	This is a generator of tuples, where the first element is the file object
	and the second element is a boolean which is True if this module opened the
	file (and thus should close it).

	Raises
	------
	OSError : Accessing the given file path failed

	Parameters
	----------
	files : Union[str, bytes, os.PathLike, io.IOBase, int, collections.abc.Iterable]
		Collection or single instance of a filepath and file-like object
	"""
	if not isinstance(files, path_types) and not hasattr(files, "read"):
		for f in files:
			yield clean_file(f)
	else:
		yield clean_file(files) 
开发者ID:ipfs-shipyard,项目名称:py-ipfs-http-client,代码行数:23,代码来源:utils.py

示例11: _update_sidecar

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def _update_sidecar(sidecar_fname, key, val):
    """Update a sidecar JSON file with a given key/value pair.

    Parameters
    ----------
    sidecar_fname : str | os.PathLike
        Full name of the data file
    key : str
        The key in the sidecar JSON file. E.g. "PowerLineFrequency"
    val : str
        The corresponding value to change to in the sidecar JSON file.
    """
    with open(sidecar_fname, "r") as fin:
        sidecar_json = json.load(fin)
    sidecar_json[key] = val
    with open(sidecar_fname, "w") as fout:
        json.dump(sidecar_json, fout) 
开发者ID:mne-tools,项目名称:mne-bids,代码行数:19,代码来源:utils.py

示例12: load

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def load(source_dir: Union[os.PathLike, str]) -> Any:
    """
    Load an object from a directory, saved by
    ``gordo.serializer.pipeline_serializer.dump``

    This take a directory, which is either top-level, meaning it contains
    a sub directory in the naming scheme: "n_step=<int>-class=<path.to.Class>"
    or the aforementioned naming scheme directory directly. Will return that
    unsterilized object.


    Parameters
    ----------
    source_dir: Union[os.PathLike, str]
        Location of the top level dir the pipeline was saved

    Returns
    -------
    Union[GordoBase, Pipeline, BaseEstimator]
    """
    # This source dir should have a single pipeline entry directory.
    # may have been passed a top level dir, containing such an entry:
    with open(os.path.join(source_dir, "model.pkl"), "rb") as f:
        return pickle.load(f) 
开发者ID:equinor,项目名称:gordo,代码行数:26,代码来源:serializer.py

示例13: read_data_cache

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def read_data_cache(
    cache_key: bytes,
    path: os.PathLike,
    *,
    pickled: bool=True,
    source_dir: Optional[pathlib.Path] = None,
) -> Any:
    if source_dir is None:
        source_dir = get_shared_data_dir_path()
    full_path = source_dir / path

    if full_path.exists():
        with open(full_path, 'rb') as f:
            src_hash = f.read(len(cache_key))
            if src_hash == cache_key:
                if pickled:
                    data = f.read()
                    try:
                        return pickle.loads(data)
                    except Exception:
                        logging.exception(f'could not unpickle {path}')
                else:
                    return f.read() 
开发者ID:edgedb,项目名称:edgedb,代码行数:25,代码来源:buildmeta.py

示例14: read

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def read(self, filenames, encoding=None):
        """Read and parse a filename or an iterable of filenames.

        Files that cannot be opened are silently ignored; this is
        designed so that you can specify an iterable of potential
        configuration file locations (e.g. current directory, user's
        home directory, systemwide directory), and all existing
        configuration files in the iterable will be read.  A single
        filename may also be given.

        Return list of successfully read files.
        """
        if isinstance(filenames, (str, bytes, os.PathLike)):
            filenames = [filenames]
        read_ok = []
        for filename in filenames:
            try:
                with open(filename, encoding=encoding) as fp:
                    self._read(fp, filename)
            except OSError:
                continue
            if isinstance(filename, os.PathLike):
                filename = os.fspath(filename)
            read_ok.append(filename)
        return read_ok 
开发者ID:luoliyan,项目名称:chinese-support-redux,代码行数:27,代码来源:configparser.py

示例15: setUp

# 需要导入模块: import os [as 别名]
# 或者: from os import PathLike [as 别名]
def setUp(self):
        super().setUp()

        # Mock os.path.basename() because since Python 3.6, it fails with an
        # error when a mock is passed to it ("TypeError: expected str, bytes or
        # os.PathLike object, not MagicMock").
        self.os_path_basename_mock = mock.Mock()
        self.patch(
            'retdec.tools.decompiler.os.path.basename',
            self.os_path_basename_mock
        )

        # Mock Decompiler so that when it is instantiated, it returns our
        # decompiler that can be used in the tests.
        self.decompiler = mock.MagicMock(spec_set=Decompiler)
        self.DecompilerMock = mock.Mock()
        self.DecompilerMock.return_value = self.decompiler
        self.patch(
            'retdec.tools.decompiler.Decompiler',
            self.DecompilerMock
        ) 
开发者ID:s3rvac,项目名称:retdec-python,代码行数:23,代码来源:decompiler_tests.py


注:本文中的os.PathLike方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。