Skip to content
This repository has been archived by the owner on Feb 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #140 from jcrist/isdir-isfile
Browse files Browse the repository at this point in the history
Add isdir and isfile
  • Loading branch information
martindurant committed Nov 13, 2017
2 parents cb53d08 + 48c334e commit 5fda481
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions hdfs3/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ def info(self, path):
_lib.hdfsFreeFileInfo(ctypes.byref(fi), 1)
return ensure_string(out)

def isdir(self, path):
"""Return True if path refers to an existing directory."""
try:
info = self.info(path)
return info['kind'] == 'directory'
except EnvironmentError:
return False

def isfile(self, path):
"""Return True if path refers to an existing file."""
try:
info = self.info(path)
return info['kind'] == 'file'
except EnvironmentError:
return False

def walk(self, path):
""" Get all file entries below given path """
return ([ensure_trailing_slash(ensure_string(path), False)]
Expand Down
19 changes: 19 additions & 0 deletions hdfs3/tests/test_hdfs3.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ def test_info(hdfs):
assert hdfs.info('/')['kind'] == 'directory'


def test_isdir_isfile(hdfs):
fn = '/tmp/test/a'
dir = '/tmp/test'
missing = '/tmp/not_a_real_path'

with hdfs.open(fn, 'wb', replication=1) as f:
f.write('a' * 5)

# isdir
assert hdfs.isdir(dir)
assert not hdfs.isdir(fn)
assert not hdfs.isdir(missing)

# isfile
assert hdfs.isfile(fn)
assert not hdfs.isfile(dir)
assert not hdfs.isfile(missing)


def test_df(hdfs):
with hdfs.open(a, 'wb', replication=1) as f:
f.write('a' * 10)
Expand Down

0 comments on commit 5fda481

Please sign in to comment.