簡體   English   中英

'builtin_function_or_method'列出

[英]'builtin_function_or_method' to list

我了解Python是一種動態語言的事實,但是下面的代碼困擾着我。

我有下面的簡單程序,它具有一些輔助功能來包裝命令執行。

EventLoaderToVerticaHelper是帶有兩個方法的幫助程序類,因此,當我調用“ get_filenames_from_hdfs_with_filter”時,它應該引發錯誤或返回String列表。

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) 

下面的代碼使用上面的幫助器,

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

當我運行上述程序時,出現以下錯誤。 我確定如果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

我知道我期望它表現出某種類型化的語言的行為...當出現異常情況時,我想方法返回List [Str]以終止程序。

我怎么能做到這一點,我嘗試了返回類型和其他東西,但沒有運氣。

return p.stdout.readlines

應該代替

return p.stdout.readlines()

請注意,從stderr讀取時,您確實正確地在上面兩行調用了readlines。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM