0

I'm getting a weird error on some of my scripts. These worked like last week with the same code.

My code:

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))

And I am getting the error:

  File "Vtst-linux.py", line 46, in <module>
    print("This is lowest mask: {}".format(lwmask))
NameError: name 'lwmask' is not defined

I am getting this error on Linux Ubuntu Python versions 3.4.1 and 3.4.0 but not Windows on with the same Python versions. I have tried this on virtual environments on the Linux server and just running it from the install path with the same results. The input file is basically a bunch of IP networks in CIDR notation.

Any help is appreciated, I am baffled by this and I have tried changing the variable name and other things and it hasn't helped.

1
  • Your code is doomed to fail with empty files, since the for loop where you define lwmask wont be executed. Are you sure that you are using a non-empty file as input? Does print(lwmask) produce output on linux? Commented Jun 11, 2014 at 16:50

1 Answer 1

2

The variable lwmask is not defined and you try to use it.

This could happen, if the for line in read: loop does not contain any item and inner block defining lwmask is not executed.

Now you shall research, why it does not contain any line.

Sign up to request clarification or add additional context in comments.

3 Comments

Not certain, but isn't this a scope issue with python 3? I.e. lwmask leaves scope after the for loop, doesn't it?
@GoingTharn No. A for loop does not create a local scope.
Yeah, the file was empty. I thought I had checked. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.