Continue from given folder when walking recursively through folder

Viewed 27

I'm walking recursively through a folder using json files that contain metadata to tag some ebook files and creating a .ts folder with other json files. The process has stopped with an error and I don't want to start from zero. Is there any way given the last processed folder to continue from there? By continue I mean to not do any processing until that folder is visited.

def process_files_not_in_hidden_folder(path: str, extension: str) -> None:
    """
    Get all files recursively from parent folder,
    except for the ones that are in hidden folders
    """
    for root, subdirs, filenames in os.walk(path):
        subdirs[:] = [d for d in subdirs if not d[0] == '.']
        for filename in filenames:
            if filename.endswith(extension):
                meta_file = os.path.join(root, filename)
                export_tags(meta_file)
1 Answers

Didn't tested.

You can use variable with start folder and run code only if it None.

And when it is not None then compare with root and set it None when they are the same.

Something like this.

def process_files_not_in_hidden_folder(path: str, extension: str, start_folder=None) -> None:
    """
    Get all files recursively from parent folder,
    except for the ones that are in hidden folders
    """
        
    for root, subdirs, filenames in os.walk(path):
        
        # search `start_folder` (if not `None`)
        if start_folder and start_folder == root:
           start_folder = None
                
        # skip if it didn't find `start_folder`
        if not start_folder:  # it can't be `else`
            subdirs[:] = [d for d in subdirs if not d[0] == '.']
            for filename in filenames:
                if filename.endswith(extension):
                    meta_file = os.path.join(root, filename)
                    export_tags(meta_file)

BTW:

If your files can make problem then you could add code in try/except to catch error and continue code with next folder.

And if your code creates some file for every folder then you could skip folder if file already exists.

Related