當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。