本文整理汇总了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)
示例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
示例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
示例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
示例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
示例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
示例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