Man*_*esh 1 python python-2.7 python-3.x
我理解 Python 是一种动态语言的事实,但下面的代码让我很困扰。
我有下面的简单程序,它有一些帮助函数来包装命令执行。
EventLoaderToVerticaHelper 是一个有两种方法的辅助类,所以当我调用“get_filenames_from_hdfs_with_filter”时,它应该抛出一个错误或返回一个字符串列表。
class EventLoaderToVerticaHelper:
def __init__(self):
pass
@staticmethod
def execute_command(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
error_lines = p.stderr.readlines()
if len(error_lines) > 0:
raise error_lines
return p.stdout.readlines
@staticmethod
def get_filenames_from_hdfs_with_filter(folder, filetype):
cmd = "hdfs dfs -ls {0}/*.{1} | awk '{print $8}'".replace("{0}", folder).replace("{1}", filetype)
return EventLoaderToVerticaHelper.execute_command(cmd)
Run Code Online (Sandbox Code Playgroud)
下面的代码使用了上面的助手,
from src.EventLoaderToVerticaHelper import EventLoaderToVerticaHelper
if __name__ == '__main__':
filelist = EventLoaderToVerticaHelper.get_filenames_from_hdfs_with_filter("/user/cloudera/testfiles", "csv")
for s in filelist:
print s
Run Code Online (Sandbox Code Playgroud)
当我运行上述程序时,出现以下错误。我确定返回类型如果 List[Str]
Traceback (most recent call last):
File "/home/cloudera/PycharmProjects/vertical-event-loader/src/EventLoaderToVertica.py", line 29, in <module>
for s in filelist:
TypeError: 'builtin_function_or_method' object is not iterable
Run Code Online (Sandbox Code Playgroud)
我知道我期望它表现出一种类型化语言......我想方法返回 List[Str],当出现异常时我想终止程序。
我怎样才能做到这一点,我尝试了返回类型和其他东西,但没有运气。
return p.stdout.readlines
Run Code Online (Sandbox Code Playgroud)
应该是
return p.stdout.readlines()
Run Code Online (Sandbox Code Playgroud)
请注意,当您从 stderr 读取时,您确实正确调用了上面两行的 readlines。
| 归档时间: |
|
| 查看次数: |
1592 次 |
| 最近记录: |