gpt4 book ai didi

python - 使用子进程输出到 HDFS 中的文件

转载 作者:太空宇宙 更新时间:2023-11-04 07:40:43 24 4
gpt4 key购买 nike

我有一个脚本,它逐行读取文本,稍微修改该行,然后将该行输出到文件。我可以很好地将文本读入文件,问题是我无法输出文本。这是我的代码。

cat = subprocess.Popen(["hadoop", "fs", "-cat", "/user/test/myfile.txt"], stdout=subprocess.PIPE)
for line in cat.stdout:
line = line+"Blah";
subprocess.Popen(["hadoop", "fs", "-put", "/user/test/moddedfile.txt"], stdin=line)

这是我遇到的错误。

AttributeError: 'str' object has no attribute 'fileno'
cat: Unable to write to output stream.

最佳答案

stdin 参数不接受字符串。它应该是 PIPENone 或现有文件(具有有效 .fileno() 或整数文件描述符的文件)。

from subprocess import Popen, PIPE

cat = Popen(["hadoop", "fs", "-cat", "/user/test/myfile.txt"],
stdout=PIPE, bufsize=-1)
put = Popen(["hadoop", "fs", "-put", "-", "/user/test/moddedfile.txt"],
stdin=PIPE, bufsize=-1)
for line in cat.stdout:
line += "Blah"
put.stdin.write(line)

cat.stdout.close()
cat.wait()
put.stdin.close()
put.wait()

关于python - 使用子进程输出到 HDFS 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22349733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com