list - python generator yield for a file -
i want use generator script alwayse exception:
traceback (most recent call last): file "scan.py", line 56, in scan_m line in ip_list: file "scan", line 46, in expand_ip_addr yield str(ip) + '\n' unboundlocalerror: local variable 'ip' referenced before assignment
and here code, should change? had never used generator before.
def expand_ip_addr(): open('ip.txt', 'r') file_in: #remove blank lines line in file_in: if '/' in str(line): ip in ipnetwork(str(line)): yield str(ip else: line def scan_m(ip_list): = 1 try: line in ip_list: # line = + 1 def main(): ip_list = expand_ip_addr() scan_m(ip_list) if __name__ == "__main__": main()
update mistake else statement wrong, sry
change
for line in file_in.read().replace('\n\n', '\n'):
and
else: yield str(ip) + '\n'
to
for line in file_in: line = line.strip() if not line: continue
and
else: yield line + '\n'
Comments
Post a Comment