如何使用python检查HDFS中的文件

2024-09-14 11:34:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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中时,它向我抛出以下错误:

^{pr2}$

如何解决这个问题?在


Tags: runimportcmddefargscodeprocsystem
1条回答
网友
1楼 · 发布于 2024-09-14 11:34:25

我将使用api而不是调用子进程。为此,最好使用api,例如spotify创建的snakebite。此示例检查给定文件夹中是否存在文件:

from snakebite.client import Client
client = Client("localhost", 8020, use_trash=False)
return "fileName" in client.ls(['hdfs_path'])

相关问题 更多 >