正在尝试为 SATCOM iDirect 调制解调器构建解析器 / 故障记录器。
更具体地说,我目前被困在“f 中的 x:”。如果文件路径静态分配给单个文件,但不是在 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

您没有在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='')
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(18条)