Datasets:
File size: 860 Bytes
0678faf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import os
import subprocess
from typing import List
def listdir(path: str) -> List[str]:
"""
List directory. Supports either hdfs or local path. Returns full path.
Examples:
- listdir("hdfs://dir") -> ["hdfs://dir/file1", "hdfs://dir/file2"]
- listdir("/dir") -> ["/dir/file1", "/dir/file2"]
"""
files = []
if path.startswith("hdfs://"):
pipe = subprocess.Popen(args=["hdfs", "dfs", "-ls", path], shell=False, stdout=subprocess.PIPE)
for line in pipe.stdout:
parts = line.strip().split()
# drwxr-xr-x - user group 4 file
if len(parts) < 5:
continue
files.append(parts[-1].decode("utf8"))
pipe.stdout.close()
pipe.wait()
else:
files = [os.path.join(path, file) for file in os.listdir(path)]
return files
|