I have inherited a python script which was written with 2.X in mind. I have gone through making it compatible with 3.9.12. The script digs through folders and parses various excel and text files. Due to its enormity, I do not know the details of every try/except block or loop within.
The essential structure is like this (note that there are lots of loops I purposefully left out because they are irrelevant to the variables I had issues with);
if data_update == 1:
for filename in glob.glob(filemove):
newfilename2 = str(newfilename3[0])
newcsvfilepath = newfilename2+"_"+CSVT+".csv"
data_update_file = newcsvfilepath
data_update_fileread = newcsvfilepath
try:
try:
for line in read_it.splitlines():
name_index_count = 0
for y in name_dex:
name_index_count = name_index_count + 1
except:
failp = str(name_index_count)
if start_New == 1:
filenameGo = data_update_fileread
The errors I get are;
1.
line 2391, in <module>
failp = [str(ExTetF), deltamsg444, newfilecountLOG, newfilefailcountLOG, "TryFail", str(Print_List[2]), str(Print_List[10]), str(name_index_count), str(index), str(Store_Index), Parser, filename_norm, yy, linstr]
NameError: name 'name_index_count' is not defined
To me, it all looks fine. I wouldn't know where to start. These are the only locations the two variables in error are defined.
In your except
block you reference name_index_count
but it's defined in the try
block before it. If the try
block fails before name_index_count = 0
is run (perhaps an exception on read_it.splitlines()
), then that variable won't be declared in the except
block. To fix, try setting name_index_count
before the try/except section.
For the second error, if glob.glob(fileremove)
returns an empty list, then newcsvfilepath
won't be defined. Try setting it to None or an empty string before the for
block. Combining these should look like:
if data_update == 1:
newcsvfilepath = ""
for filename in glob.glob(filemove):
newfilename2 = str(newfilename3[0])
newcsvfilepath = newfilename2+"_"+CSVT+".csv"
data_update_file = newcsvfilepath
data_update_fileread = newcsvfilepath
try:
name_index_count = 0
try:
for line in read_it.splitlines():
for y in name_dex:
name_index_count = name_index_count + 1
except:
failp = str(name_index_count)