I am trying to test if a path exist in hadoop using python script.
hdfs dfs -test -e /path/to/file
The above will return 0 if path exists, 1 if path doesn't exist. Below is my python script:
if subprocess.call("hdfs dfs -test -e /path/to/file", shell = True) == 0:
# do something
The above code it's not working, subprocess is always 0 b/c it's checking the command status not the returned value. I found this post, but didn't seem to work. I also tried storing the return value of echoresult = subprocess.call("echo $?", shell = True)
, also didn't work.
Below is my full code:
#!/usr/bin/env python
import subprocess
HDFS = "hdfs dfs "
def run_commands(func, path):
subprocess.call(HDFS + func + path, shell = True)
def path_exist(path):
return True if run_commands("-test -e ", path) == 0 else False
path_exist("/path/to/file")