Skip to content
Advertisement

If then loop exits without populating list

I’m trying to use os.walk to iterate through a series of folders, find the earliest excel file in a given folder, and copy it to a temporary folder. This loop keeps exiting prior to creating the matches list. I’m getting the error

NameError: name 'xlsxExt' is not defined

How can I get the loop to run through each file in the expFolder, create one ‘matches’ list for each expFolder, and sort it? Any help would be appreciate!

path = r'C:Users...'
tempFolder = os.mkdir(os.path.join(path, 'tempFolder'))
for dataFolder, expFolder, file in os.walk(path):
    print("Starting", dataFolder)
    matches = []
    for file_name in file:
        if file_name.endswith('.xlsx'):
            xlsxExt = os.path.join(dataFolder, file_name)
    matches.append(xlsxExt) #breaks here
    sortedMatches = sorted(matches, key=os.path.getmtime)
    first = sortedMatches[0][0:]
    print(first)

Advertisement

Answer

I think there are two issues with your code.

The first is that your append call is happening outside of the inner loop. This means that you only append the last value you saw from the loop, not all the values that have the right extension.

The second issue is that you don’t properly deal with folders that have no matching files. This is the current cause of your exception, though the exact exception details would change if you move the append call as suggested above. If you avoid the NameError, you’d instead get an IndexError when you try to do sortedMatches[0][0:] when sortedMatches is empty.

I suggest something like this:

for dataFolder, expFolder, file in os.walk(path):
    print("Starting", dataFolder)
    matches = []
    for file_name in file:
        if file_name.endswith('.xlsx'):
            xlsxExt = os.path.join(dataFolder, file_name)
            matches.append(xlsxExt)                           # indent this line!
    if matches:                                               # add this, ignores empty lists
        sortedMatches = sorted(matches, key=os.path.getmtime) # indent the rest
        first = sortedMatches[0] # you don't need to slice here
        print(first)

As I commented, you don’t need the slice when getting first from sortedMatches. You were just copying the whole filename string, which is entirely unnecessary. You don’t need to make that change, it’s unrelated to your errors.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement