内置函数或方法对象不能使用os.walk

2024-06-19 19:42:45 发布

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

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

正在寻求帮助。更具体地说,我现在被困在“f中的x”这一行。如果静态地将文件路径分配给单个文件,但在os.walk循环期间不分配,则此语法有效

当前接收到错误,“回溯(最近一次呼叫): 文件“C:\Users\Desktop\ACM\u Scan\u Tool.py”,第17行,在 对于f中的x: TypeError:“内置函数”或“方法”对象不可编辑

#*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


Tags: 文件inforifosfilesusersfile
1条回答
网友
1楼 · 发布于 2024-06-19 19:42:45

您没有调用open行中的f = 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='')

相关问题 更多 >