我的一些脚本出现奇怪的错误。这些工作与上周一样,使用相同的代码。
我的代码:
import ipaddress
import csv
from csv import DictReader, DictWriter
import operator
import time
import os
import datetime
from datetime import datetime
cnt=1
FMT='%a %b %d %H:%M:%S %Y'
start_time=str(time.strftime("%c", time.localtime()))
forpth=r'/home/path/Geo_Assigned'
forfiles=[os.path.join(forpth, fname) for fname in os.listdir(forpth)if fname.startswith('DC')]
forlat=max(forfiles, key=os.path.getmtime)
mskcmlst=['255.255.255.254', '255.255.255.252', '255.255.255.248', '255.255.255.240', '255.255.255.224', '255.255.255.192', '255.255.255.128', '255.255.255.0', '255.255.254.0', '255.255.252.0', '255.255.248.0', '255.255.240.0', '255.255.224.0', '255.255.192.0', '255.255.128.0', '255.255.0.0', '255.254.0.0', '255.252.0.0', '255.248.0.0', '255.240.0.0', '255.224.0.0', '255.192.0.0', '255.128.0.0', '255.0.0.0']
alphlst=[]
WaitList=[]
cmpmask=ipaddress.ip_address('255.255.255.254')
msk32=ipaddress.ip_address('255.255.255.255')
n=0
with open(forlat, newline='') as fin:
read = DictReader(fin)
for line in read:
line['CIDR']=ipaddress.ip_network(line['CIDR'])
line['Mask']=line['CIDR'].netmask
lwmask=str(line['Mask'])
alphlst.append(line)
print(lwmask)
print("This is lowest mask: {}".format(lwmask))
endnum=mskcmlst.index(lwmask)+1
print("This is endnum {}".format(endnum))
我收到错误:
File "Vtst-linux.py", line 46, in <module>
print("This is lowest mask: {}".format(lwmask))
NameError: name 'lwmask' is not defined
我在 Linux Ubuntu Python 版本 3.4.1 和 3.4.0 上遇到此错误,但在具有相同 Python 版本的 Windows 上却没有。我已经在 Linux 服务器上的虚拟环境中尝试过这个,并且只是从安装路径运行它,结果相同。输入文件基本上是一堆采用 CIDR 表示法的 IP 网络。
感谢任何帮助,我对此感到困惑,我尝试更改变量名称和其他内容,但没有帮助。
最佳答案
变量 lwmask
未定义,您尝试使用它。
如果 for line in read:
循环不包含任何项并且定义 lwmask
的内部 block 未执行,则可能会发生这种情况。
现在你要研究一下,为什么它不包含任何一行。
关于Python 名称错误 : Name is not defined Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24168315/