【问题标题】:builtin_function_or_method' object is not iterable using os.walkbuiltin_function_or_method' 对象不能使用 os.walk 进行迭代
【发布时间】:2020-03-15 09:29:02
【问题描述】:

正在尝试为 SATCOM iDirect 调制解调器构建解析器/故障记录器。

寻求帮助。更具体地说,我目前被困在线,“for x in f:”。如果文件路径静态分配给单个文件,则此语法有效,但在 os.walk 循环期间无效

当前收到错误,”Traceback(最近一次调用最后一次): 文件“C:\Users\Desktop\ACM_Scan_Tool.py”,第 17 行,在 对于 f 中的 x: TypeError: 'builtin_function_or_method' 对象不可迭代

#*My current code*

import os
rootdir = (r"C:\Users\Desktop\AUB_AES_ACM")

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
       if file.endswith(".log"):
            print(file)
            f = open
            **for x in f:**
                if x.rfind('OpenAMIP: received <-- w 0 0 0 0 0 0 0 0 0 0') >-1 :
                    print(x, end='')
            f.close
        else:
            continue

【问题讨论】:

    标签: python loops parsing os.walk


    【解决方案1】:

    您没有在f = open 行中调用open 函数,因此您从未打开过该文件。应该是f = open(file)。或者你可以使用 with 语句。

    print(file)
    with open(file) as f:
        for x in f:
            if x.rfind('OpenAMIP: received <-- w 0 0 0 0 0 0 0 0 0 0') >-1 :
                print(x, end='')
    

    【讨论】:

    • 谢谢,并注明。我最终只是创建了一个路径列表,然后使用 he follwowing:with open(readf,"r") as f: count = 0 for line in f: count = count + 1 if count == 1:跨度>
    猜你喜欢
    • 2019-01-12
    • 2015-07-20
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多