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


Python os.replace方法代码示例

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


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

示例1: _register_output_transfer_id_map_save_at_exit

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def _register_output_transfer_id_map_save_at_exit(presentation: pyuavcan.presentation.Presentation) -> None:
        # We MUST sample the configuration early because if this is a redundant transport it may reset its
        # reported descriptor and local node-ID back to default after close().
        local_node_id = presentation.transport.local_node_id
        descriptor = presentation.transport.descriptor

        def do_save_at_exit() -> None:
            if local_node_id is not None:
                file_path = _get_output_transfer_id_file_path(local_node_id, descriptor)
                tmp_path = f'{file_path}.{os.getpid()}.{time.time_ns()}.tmp'
                _logger.debug('Output TID map save: %s --> %s', tmp_path, file_path)
                with open(tmp_path, 'wb') as f:
                    pickle.dump(presentation.output_transfer_id_map, f)
                # We use replace for compatibility reasons. On POSIX, a call to rename() will be made, which is
                # guaranteed to be atomic. On Windows this may fall back to non-atomic copy, which is still
                # acceptable for us here. If the file ends up being damaged, we'll simply ignore it at next startup.
                os.replace(tmp_path, file_path)
                try:
                    os.unlink(tmp_path)
                except OSError:
                    pass
            else:
                _logger.debug('Output TID map NOT saved because the transport instance is anonymous')

        atexit.register(do_save_at_exit) 
开发者ID:UAVCAN,项目名称:pyuavcan,代码行数:27,代码来源:node.py

示例2: updateFile

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def updateFile(self, name, content, encoding=None, newline=None):
        newName = name+".new"
        oldName = name+".old"
        with open(newName, "w", encoding=encoding, newline=newline) as f:
            f.write(content)
        with open(newName, "rb") as f:
            newContent = f.read()
        try:
            with open(oldName, "rb") as f:
                oldContent = f.read()
        except OSError:
            oldContent = None

        if oldContent != newContent:
            os.replace(newName, name)
            with open(oldName, "wb") as f:
                f.write(newContent)
        else:
            os.remove(newName) 
开发者ID:BobBuildTool,项目名称:bob,代码行数:21,代码来源:common.py

示例3: save

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def save(path: str, trajectories: Sequence[TrajectoryWithRew]) -> None:
    """Generate policy rollouts and save them to a pickled list of trajectories.

    The `.infos` field of each Trajectory is set to `None` to save space.

    Args:
      path: Rollouts are saved to this path.
      venv: The vectorized environments.
      sample_until: End condition for rollout sampling.
      unwrap: If True, then save original observations and rewards (instead of
        potentially wrapped observations and rewards) by calling
        `unwrap_traj()`.
      exclude_infos: If True, then exclude `infos` from pickle by setting
        this field to None. Excluding `infos` can save a lot of space during
        pickles.
      verbose: If True, then print out rollout stats before saving.
      deterministic_policy: Argument from `generate_trajectories`.
    """
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path + ".tmp", "wb") as f:
        pickle.dump(trajectories, f)
    # Ensure atomic write
    os.replace(path + ".tmp", path)
    tf.logging.info("Dumped demonstrations to {}.".format(path)) 
开发者ID:HumanCompatibleAI,项目名称:imitation,代码行数:26,代码来源:types.py

示例4: concurrency_safe_rename

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def concurrency_safe_rename(src, dst):
        """Renames ``src`` into ``dst`` overwriting ``dst`` if it exists.

        On Windows os.replace (or for Python 2.7 its implementation
        through MoveFileExW) can yield permission errors if executed by
        two different processes.
        """
        max_sleep_time = 1
        total_sleep_time = 0
        sleep_time = 0.001
        while total_sleep_time < max_sleep_time:
            try:
                replace(src, dst)
                break
            except Exception as exc:
                if getattr(exc, 'winerror', None) == error_access_denied:
                    time.sleep(sleep_time)
                    total_sleep_time += sleep_time
                    sleep_time *= 2
                else:
                    raise
        else:
            raise 
开发者ID:flennerhag,项目名称:mlens,代码行数:25,代码来源:backports.py

示例5: atomic_write

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def atomic_write(filepath, binary=False, fsync=False):
    """ Writeable file object that atomically updates a file (using a temporary file). In some cases (namely Python < 3.3 on Windows), this could result in an existing file being temporarily unlinked.

    :param filepath: the file path to be opened
    :param binary: whether to open the file in a binary mode instead of textual
    :param fsync: whether to force write the file to disk
    """

    tmppath = filepath + '~'
    while os.path.isfile(tmppath):
        tmppath += '~'
    try:
        with open(tmppath, 'wb' if binary else 'w') as file:
            yield file
            if fsync:
                file.flush()
                os.fsync(file.fileno())
        replace(tmppath, filepath)
    finally:
        try:
            os.remove(tmppath)
        except (IOError, OSError):
            pass 
开发者ID:ArztSamuel,项目名称:DRL_DeliveryDuel,代码行数:25,代码来源:atomic_write.py

示例6: _save

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def _save(self, data=None):
        assert self._filename is not None

        if data is None:
            data = self._as_dict()

        # Serialize data:
        blob = json.dumps(data).encode()

        if _BUFFERED_MODE > 0:
            _store_in_buffer(self._filename, blob)
        else:   # Saving to disk:
            if self._write_concern:
                dirname, filename = os.path.split(self._filename)
                fn_tmp = os.path.join(dirname, '._{uid}_{fn}'.format(
                    uid=uuid.uuid4(), fn=filename))
                with open(fn_tmp, 'wb') as tmpfile:
                    tmpfile.write(blob)
                os.replace(fn_tmp, self._filename)
            else:
                with open(self._filename, 'wb') as file:
                    file.write(blob) 
开发者ID:glotzerlab,项目名称:signac,代码行数:24,代码来源:jsondict.py

示例7: __setitem__

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def __setitem__(self, key, value):
        self._validate_key(key)
        tmp_key = str(uuid.uuid4())
        try:
            self[tmp_key].update(value)
            os.replace(self[tmp_key].filename, self[key].filename)
        except (IOError, OSError) as error:
            if error.errno == errno.ENOENT and not len(value):
                raise ValueError("Cannot assign empty value!")
            else:
                raise error
        except Exception as error:
            try:
                del self[tmp_key]
            except KeyError:
                pass
            raise error
        else:
            del self._dict_registry[key] 
开发者ID:glotzerlab,项目名称:signac,代码行数:21,代码来源:dict_manager.py

示例8: test_export_import_tarfile

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_tarfile(self):
        target = os.path.join(self._tmp_dir.name, 'data.tar')
        for i in range(10):
            self.project.open_job(dict(a=i)).init()
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=target)
        assert len(self.project) == 10
        with TarFile(name=target) as tarfile:
            for i in range(10):
                assert 'a/{}'.format(i) in tarfile.getnames()
        os.replace(self.project.workspace(), self.project.workspace() + '~')
        assert len(self.project) == 0
        self.project.import_from(origin=target)
        assert len(self.project) == 10
        with pytest.deprecated_call():
            assert ids_before_export == list(sorted(self.project.find_job_ids())) 
开发者ID:glotzerlab,项目名称:signac,代码行数:19,代码来源:test_project.py

示例9: test_export_import_tarfile_zipped

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_tarfile_zipped(self):
        target = os.path.join(self._tmp_dir.name, 'data.tar.gz')
        for i in range(10):
            with self.project.open_job(dict(a=i)) as job:
                os.makedirs(job.fn('sub-dir'))
                with open(job.fn(os.path.join('sub-dir', 'signac_statepoint.json')), 'w') as file:
                    file.write(json.dumps({"foo": 0}))
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=target)
        assert len(self.project) == 10
        with TarFile.open(name=target, mode='r:gz') as tarfile:
            for i in range(10):
                assert 'a/{}'.format(i) in tarfile.getnames()
                assert 'a/{}/sub-dir/signac_statepoint.json'.format(i) in tarfile.getnames()
        os.replace(self.project.workspace(), self.project.workspace() + '~')
        assert len(self.project) == 0
        self.project.import_from(origin=target)
        assert len(self.project) == 10
        with pytest.deprecated_call():
            assert ids_before_export == list(sorted(self.project.find_job_ids()))
        for job in self.project:
            assert job.isfile(os.path.join('sub-dir', 'signac_statepoint.json')) 
开发者ID:glotzerlab,项目名称:signac,代码行数:25,代码来源:test_project.py

示例10: test_export_import_conflict_synced_with_args

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_conflict_synced_with_args(self):
        prefix_data = os.path.join(self._tmp_dir.name, 'data')
        for i in range(10):
            self.project.open_job(dict(a=i)).init()
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=prefix_data)
        with pytest.raises(DestinationExistsError):
            assert len(self.project.import_from(prefix_data)) == 10

        selection = list(self.project.find_jobs(dict(a=0)))
        os.replace(self.project.workspace(), self.project.workspace() + '~')
        assert len(self.project) == 0
        assert len(self.project.import_from(prefix_data,
                                            sync=dict(selection=selection))) == 10
        assert len(self.project) == 1
        assert len(self.project.find_jobs(dict(a=0))) == 1
        with pytest.deprecated_call():
            assert list(self.project.find_job_ids())[0] in ids_before_export 
开发者ID:glotzerlab,项目名称:signac,代码行数:21,代码来源:test_project.py

示例11: test_export_import_schema_callable

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_schema_callable(self):

        def my_schema(path):
            re_sep = re.escape(os.path.sep)
            m = re.match(r'.*' + re_sep + 'a' + re_sep + r'(?P<a>\d+)$', path)
            if m:
                return dict(a=int(m.groupdict()['a']))

        prefix_data = os.path.join(self._tmp_dir.name, 'data')
        for i in range(10):
            self.project.open_job(dict(a=i)).init()
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=prefix_data, copytree=os.replace)
        assert len(self.project.import_from(prefix_data, schema=my_schema)) == 10
        with pytest.deprecated_call():
            assert ids_before_export == list(sorted(self.project.find_job_ids())) 
开发者ID:glotzerlab,项目名称:signac,代码行数:19,代码来源:test_project.py

示例12: test_export_import_simple_path

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_simple_path(self):
        prefix_data = os.path.join(self._tmp_dir.name, 'data')
        for i in range(10):
            self.project.open_job(dict(a=i)).init()
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=prefix_data, copytree=os.replace)
        assert len(self.project) == 0
        assert len(os.listdir(prefix_data)) == 1
        assert len(os.listdir(os.path.join(prefix_data, 'a'))) == 10
        for i in range(10):
            assert os.path.isdir(os.path.join(prefix_data, 'a', str(i)))
        with pytest.raises(StatepointParsingError):
            self.project.import_from(origin=prefix_data, schema='a/{b:int}')
        assert len(self.project.import_from(prefix_data)) == 10
        assert len(self.project) == 10
        with pytest.deprecated_call():
            assert ids_before_export == list(sorted(self.project.find_job_ids())) 
开发者ID:glotzerlab,项目名称:signac,代码行数:20,代码来源:test_project.py

示例13: test_export_import_simple_path_with_float

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_simple_path_with_float(self):
        prefix_data = os.path.join(self._tmp_dir.name, 'data')
        for i in range(10):
            self.project.open_job(dict(a=float(i))).init()
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=prefix_data, copytree=os.replace)
        assert len(self.project) == 0
        assert len(os.listdir(prefix_data)) == 1
        assert len(os.listdir(os.path.join(prefix_data, 'a'))) == 10
        for i in range(10):
            assert os.path.isdir(os.path.join(prefix_data, 'a', str(float(i))))
        assert len(self.project.import_from(prefix_data)) == 10
        assert len(self.project) == 10
        with pytest.deprecated_call():
            assert ids_before_export == list(sorted(self.project.find_job_ids())) 
开发者ID:glotzerlab,项目名称:signac,代码行数:18,代码来源:test_project.py

示例14: test_export_import_complex_path

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_complex_path(self):
        prefix_data = os.path.join(self._tmp_dir.name, 'data')
        sp_0 = [{'a': i, 'b': i % 3} for i in range(5)]
        sp_1 = [{'a': i, 'b': i % 3, 'c': {'a': i, 'b': 0}} for i in range(5)]
        sp_2 = [{'a': i, 'b': i % 3, 'c': {'a': i, 'b': 0, 'c': {'a': i, 'b': 0}}}
                for i in range(5)]
        statepoints = sp_0 + sp_1 + sp_2
        for sp in statepoints:
            self.project.open_job(sp).init()
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=prefix_data, copytree=os.replace)
        assert len(self.project) == 0
        self.project.import_from(prefix_data)
        assert len(self.project) == len(statepoints)
        with pytest.deprecated_call():
            assert ids_before_export == list(sorted(self.project.find_job_ids())) 
开发者ID:glotzerlab,项目名称:signac,代码行数:19,代码来源:test_project.py

示例15: test_export_import_simple_path_schema_from_path

# 需要导入模块: import os [as 别名]
# 或者: from os import replace [as 别名]
def test_export_import_simple_path_schema_from_path(self):
        prefix_data = os.path.join(self._tmp_dir.name, 'data')
        for i in range(10):
            self.project.open_job(dict(a=i)).init()
        with pytest.deprecated_call():
            ids_before_export = list(sorted(self.project.find_job_ids()))
        self.project.export_to(target=prefix_data, copytree=os.replace)
        assert len(self.project) == 0
        assert len(os.listdir(prefix_data)) == 1
        assert len(os.listdir(os.path.join(prefix_data, 'a'))) == 10
        for i in range(10):
            assert os.path.isdir(os.path.join(prefix_data, 'a', str(i)))
        ret = self.project.import_from(origin=prefix_data, schema='a/{a:int}')
        assert len(ret) == 10
        with pytest.deprecated_call():
            assert ids_before_export == list(sorted(self.project.find_job_ids())) 
开发者ID:glotzerlab,项目名称:signac,代码行数:18,代码来源:test_project.py


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