import subprocess
def run_cmd(args_list):
print('Running system command: {0}'.format(' '.join(args_list)))
proc = subprocess.Popen(args_list, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
proc.communicate()
return proc.returncode
cmd = ['hadoop', 'fs', '-test', '-e', hdfs_file_path]
code = run_cmd(cmd)
if code:
print 'file not exist'
当我给出此命令以查看HDFS中是否存在文件时,会抛出此错误:
RuntimeError: Error running command: hadoop fs -test -f /app/tmp/1.json. Return code: 1, Error: b''
如何解决此问题?
答案 0 :(得分:0)
我将使用api而不是调用子流程。为此最好使用一个API,例如由Spotify创建的snakebite。此示例检查给定文件夹中是否存在文件:
from snakebite.client import Client
client = Client("localhost", 8020, use_trash=False)
return "fileName" in client.ls(['hdfs_path'])