當前位置: 首頁>>代碼示例>>Python>>正文


Python posixpath.isdir方法代碼示例

本文整理匯總了Python中posixpath.isdir方法的典型用法代碼示例。如果您正苦於以下問題:Python posixpath.isdir方法的具體用法?Python posixpath.isdir怎麽用?Python posixpath.isdir使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在posixpath的用法示例。


在下文中一共展示了posixpath.isdir方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_isdir

# 需要導入模塊: import posixpath [as 別名]
# 或者: from posixpath import isdir [as 別名]
def test_isdir(self):
        self.assertIs(posixpath.isdir(test_support.TESTFN), False)
        f = open(test_support.TESTFN, "wb")
        try:
            f.write("foo")
            f.close()
            self.assertIs(posixpath.isdir(test_support.TESTFN), False)
            os.remove(test_support.TESTFN)
            os.mkdir(test_support.TESTFN)
            self.assertIs(posixpath.isdir(test_support.TESTFN), True)
            os.rmdir(test_support.TESTFN)
        finally:
            if not f.close():
                f.close()
            try:
                os.remove(test_support.TESTFN)
            except os.error:
                pass
            try:
                os.rmdir(test_support.TESTFN)
            except os.error:
                pass

        self.assertRaises(TypeError, posixpath.isdir) 
開發者ID:ofermend,項目名稱:medicare-demo,代碼行數:26,代碼來源:test_posixpath.py

示例2: makedirs

# 需要導入模塊: import posixpath [as 別名]
# 或者: from posixpath import isdir [as 別名]
def makedirs(name, mode=0o777, exist_ok=False):
    """makedirs(name [, mode=0o777][, exist_ok=False])

    Super-mkdir; create a leaf directory and all intermediate ones.  Works like
    mkdir, except that any intermediate path segment (not just the rightmost)
    will be created if it does not exist. If the target directory already
    exists, raise an OSError if exist_ok is False. Otherwise no exception is
    raised.  This is recursive.

    """
    head, tail = path.split(name)
    if not tail:
        head, tail = path.split(head)
    if head and tail and not path.exists(head):
        try:
            makedirs(head, mode, exist_ok)
        except FileExistsError:
            # Defeats race condition when another thread created the path
            pass
        cdir = curdir
        if isinstance(tail, bytes):
            cdir = bytes(curdir, 'ASCII')
        if tail == cdir:           # xxx/newdir/. exists if xxx/newdir exists
            return
    try:
        mkdir(name, mode)
    except OSError:
        # Cannot rely on checking for EEXIST, since the operating system
        # could give priority to other errors like EACCES or EROFS
        if not exist_ok or not path.isdir(name):
            raise 
開發者ID:awemulya,項目名稱:kobo-predict,代碼行數:33,代碼來源:os.py

示例3: makedirs

# 需要導入模塊: import posixpath [as 別名]
# 或者: from posixpath import isdir [as 別名]
def makedirs(name, mode=0o777, exist_ok=False):
    """makedirs(name [, mode=0o777][, exist_ok=False])

    Super-mkdir; create a leaf directory and all intermediate ones.  Works like
    mkdir, except that any intermediate path segment (not just the rightmost)
    will be created if it does not exist. If the target directory already
    exists, raise an OSError if exist_ok is False. Otherwise no exception is
    raised.  This is recursive.

    """
    head, tail = path.split(name)
    if not tail:
        head, tail = path.split(head)
    if head and tail and not path.exists(head):
        try:
            makedirs(head, exist_ok=exist_ok)
        except FileExistsError:
            # Defeats race condition when another thread created the path
            pass
        cdir = curdir
        if isinstance(tail, bytes):
            cdir = bytes(curdir, 'ASCII')
        if tail == cdir:           # xxx/newdir/. exists if xxx/newdir exists
            return
    try:
        mkdir(name, mode)
    except OSError:
        # Cannot rely on checking for EEXIST, since the operating system
        # could give priority to other errors like EACCES or EROFS
        if not exist_ok or not path.isdir(name):
            raise 
開發者ID:Relph1119,項目名稱:GraphicDesignPatternByPython,代碼行數:33,代碼來源:os.py

示例4: test_list_artifacts

# 需要導入模塊: import posixpath [as 別名]
# 或者: from posixpath import isdir [as 別名]
def test_list_artifacts(sftp_mock):
    artifact_root_path = "/experiment_id/run_id/"
    repo = SFTPArtifactRepository("sftp://test_sftp"+artifact_root_path, sftp_mock)

    # mocked file structure
    #  |- file
    #  |- model
    #     |- model.pb

    file_path = "file"
    file_size = 678
    dir_path = "model"
    sftp_mock.isdir = MagicMock(side_effect=lambda path: {
            artifact_root_path: True,
            os.path.join(artifact_root_path, file_path): False,
            os.path.join(artifact_root_path, dir_path): True,
        }[path])
    sftp_mock.listdir = MagicMock(return_value=[file_path, dir_path])

    file_stat = MagicMock()
    file_stat.configure_mock(st_size=file_size)
    sftp_mock.stat = MagicMock(return_value=file_stat)

    artifacts = repo.list_artifacts(path=None)

    sftp_mock.listdir.assert_called_once_with(artifact_root_path)
    sftp_mock.stat.assert_called_once_with(artifact_root_path + file_path)

    assert len(artifacts) == 2
    assert artifacts[0].path == file_path
    assert artifacts[0].is_dir is False
    assert artifacts[0].file_size == file_size
    assert artifacts[1].path == dir_path
    assert artifacts[1].is_dir is True
    assert artifacts[1].file_size is None 
開發者ID:mlflow,項目名稱:mlflow,代碼行數:37,代碼來源:test_sftp_artifact_repo.py

示例5: test_list_artifacts_with_subdir

# 需要導入模塊: import posixpath [as 別名]
# 或者: from posixpath import isdir [as 別名]
def test_list_artifacts_with_subdir(sftp_mock):
    artifact_root_path = "/experiment_id/run_id/"
    repo = SFTPArtifactRepository("sftp://test_sftp"+artifact_root_path, sftp_mock)

    # mocked file structure
    #  |- model
    #     |- model.pb
    #     |- variables
    dir_name = 'model'

    # list artifacts at sub directory level
    file_path = 'model.pb'
    file_size = 345
    subdir_name = 'variables'

    sftp_mock.listdir = MagicMock(return_value=[file_path, subdir_name])

    sftp_mock.isdir = MagicMock(side_effect=lambda path: {
            posixpath.join(artifact_root_path, dir_name): True,
            posixpath.join(artifact_root_path, dir_name, file_path): False,
            posixpath.join(artifact_root_path, dir_name, subdir_name): True,
        }[path])

    file_stat = MagicMock()
    file_stat.configure_mock(st_size=file_size)
    sftp_mock.stat = MagicMock(return_value=file_stat)

    artifacts = repo.list_artifacts(path=dir_name)

    sftp_mock.listdir.assert_called_once_with(artifact_root_path + dir_name)
    sftp_mock.stat.assert_called_once_with(artifact_root_path + dir_name + '/' + file_path)

    assert len(artifacts) == 2
    assert artifacts[0].path == posixpath.join(dir_name, file_path)
    assert artifacts[0].is_dir is False
    assert artifacts[0].file_size == file_size
    assert artifacts[1].path == posixpath.join(dir_name, subdir_name)
    assert artifacts[1].is_dir is True
    assert artifacts[1].file_size is None 
開發者ID:mlflow,項目名稱:mlflow,代碼行數:41,代碼來源:test_sftp_artifact_repo.py

示例6: test_log_artifacts

# 需要導入模塊: import posixpath [as 別名]
# 或者: from posixpath import isdir [as 別名]
def test_log_artifacts():
    for artifact_path in [None, "sub_dir", "very/nested/sub/dir"]:
        file_content_1 = 'A simple test artifact\nThe artifact is located in: ' + str(artifact_path)
        file_content_2 = os.urandom(300)

        file1 = "meta.yaml"
        directory = "saved_model"
        file2 = "sk_model.pickle"
        with TempDir() as local, TempDir() as remote:
            with open(os.path.join(local.path(), file1), "w") as f:
                f.write(file_content_1)
            os.mkdir(os.path.join(local.path(), directory))
            with open(os.path.join(local.path(), directory, file2), "wb") as f:
                f.write(file_content_2)

            sftp_path = "sftp://" + remote.path()
            store = SFTPArtifactRepository(sftp_path)
            store.log_artifacts(local.path(), artifact_path)

            remote_dir = posixpath.join(
                remote.path(),
                '.' if artifact_path is None else artifact_path)
            assert posixpath.isdir(remote_dir)
            assert posixpath.isdir(posixpath.join(remote_dir, directory))
            assert posixpath.isfile(posixpath.join(remote_dir, file1))
            assert posixpath.isfile(posixpath.join(remote_dir, directory, file2))

            with open(posixpath.join(remote_dir, file1), 'r') as remote_content:
                assert remote_content.read() == file_content_1

            with open(posixpath.join(remote_dir, directory, file2), 'rb') as remote_content:
                assert remote_content.read() == file_content_2 
開發者ID:mlflow,項目名稱:mlflow,代碼行數:34,代碼來源:test_sftp_artifact_repo.py

示例7: makedirs

# 需要導入模塊: import posixpath [as 別名]
# 或者: from posixpath import isdir [as 別名]
def makedirs(name, mode=0o777, exist_ok=False):
    """makedirs(path [, mode=0o777][, exist_ok=False])

    Super-mkdir; create a leaf directory and all intermediate ones.
    Works like mkdir, except that any intermediate path segment (not
    just the rightmost) will be created if it does not exist. If the
    target directory with the same mode as we specified already exists,
    raises an OSError if exist_ok is False, otherwise no exception is
    raised.  This is recursive.

    """
    head, tail = path.split(name)
    if not tail:
        head, tail = path.split(head)
    if head and tail and not path.exists(head):
        try:
            makedirs(head, mode, exist_ok)
        except OSError as e:
            # be happy if someone already created the path
            if e.errno != errno.EEXIST:
                raise
        cdir = curdir
        if isinstance(tail, bytes):
            cdir = bytes(curdir, 'ASCII')
        if tail == cdir:           # xxx/newdir/. exists if xxx/newdir exists
            return
    try:
        mkdir(name, mode)
    except OSError as e:
        dir_exists = path.isdir(name)
        expected_mode = _get_masked_mode(mode)
        if dir_exists:
            # S_ISGID is automatically copied by the OS from parent to child
            # directories on mkdir.  Don't consider it being set to be a mode
            # mismatch as mkdir does not unset it when not specified in mode.
            actual_mode = st.S_IMODE(lstat(name).st_mode) & ~st.S_ISGID
        else:
            actual_mode = -1
        if not (e.errno == errno.EEXIST and exist_ok and dir_exists and
                actual_mode == expected_mode):
            if dir_exists and actual_mode != expected_mode:
                e.strerror += ' (mode %o != expected mode %o)' % (
                        actual_mode, expected_mode)
            raise 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:46,代碼來源:os.py


注:本文中的posixpath.isdir方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。