Search code examples
pythonfilecsvfilenamesmatching

Python 'if...continue' unexpectedly returns control to parent loop of current loop


I'm using Python in Visual Studio, stepping through the code in debug mode. After the first if..continue statement is checked, control returns to for file.. as I expected.

However, on the 2nd pass through, when it drops down to the next if..continue statement, control returns to the start of the parent for dir loop, when I expected it to return to for file.. again. Goal is to ignore any subdirectories and csv files with names of the form _*.csv (I'm hoping I don't have to dig into learning additional pattern matching stuff just now - if possible need to get a current small task taken care of):

for dir in os.listdir(masterDirPath):    
    currentDir = masterDirPath + dir

    # iterate through csv logs within current data folder, aggregating data
    for file in os.listdir(currentDir):

        path = os.path.join(currentDir, file)
        if os.path.isdir(path):
            # skip directories
            continue

        if file.startswith('_'):
            # skip custom files
            continue

        if file.endswith(".csv"):
            # open log file
            .
            .
            .

Solution

  • From what I understand -- you have a list of directories in os.listdir and a list of files in each of those directories.

    Ideally, you should load ONE directory into currentDir --> this directory should go through each of the files.

    I believe the control flow is affected because you're assigning the same currentDir to all your directories.

    Try this:

    for dir in os.listdir(masterDirPath):    
        currentDir = masterDirPath + dir
    
      # iterate through csv logs within current data folder, aggregating data
      ##### indent this part of your code #####
    
         for file in os.listdir(currentDir):
            path = os.path.join(currentDir, file)
    
            if os.path.isdir(path):
               # skip directories
               continue
    
            if file.startswith('_'):
               # skip custom files
               continue
    
            if file.endswith(".csv"):
              # open log file
              .
              .
              .