# Mimic os.walk

**URL:** <https://forum.logik.tv/t/mimic-os-walk/3631>\
**Category:** Python\
**Created:** [August 11, 2021, 7:54am UTC](https://forum.logik.tv/t/mimic-os-walk/3631 "2021-08-11T07:54:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyleobley](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kyleobley](https://forum.logik.tv/u/kyleobley)\
**Post date:** [August 11, 2021, 7:54am UTC](https://forum.logik.tv/t/mimic-os-walk/3631/1 "2021-08-11T07:54:16Z")

</div>

The topic of being able to archive soft-imported clips used in a timeline, batch, whatever in the original format and not duplicated has come up many times. Basically a way to “collect files”. I thought it’d be interesting to start working on a script to do this, or at least attempt to.

That said, I’ve run into a stumbling block: How to drill down into an unknown level of sub-folders until you find either a sequence, clip or batchgroup? If this was on a normal filesystem you could easily use os.walk and let it do it’s thing but, as far as I can tell, there isn’t a way to do that within the Flame.

I feel like there’s some clever way to write some sort of loop that can continue to call itself and keep running again when a new folder is found within the current folder.

Some simple code I’ve been testing with:

```auto
def dump_entries(selection):
    import flame

    for libraries in flame.project.current_project.current_workspace.libraries:
        print ("- ", libraries.name)

        for folder in libraries.folders:
            print (" +-- ", folder.name)

            if has_subfolder(folder):
                for i in range(len(folder.folders)):
                    print (" +-- ", folder.folders[i].name)

                    if has_subfolder(folder.folders[i]):
                        for x in range(len(folder.folders[i].folders)):
                            print (" +-- ", folder.folders[i].folders[x].name)

def has_subfolder(selection):
    import flame

    if len(selection.folders) == 0:
        return False
    else:
        return True

```

Does anyone have any ideas or suggestions of what else to look at? My python skills are pretty “throw a dart at the board” style so when it comes to more complex structures and whatnot I’m pretty lost.

Any help would be greatly appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![claussteinmassl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.logik.tv/claussteinmassl/32/1922_2.png) [@claussteinmassl](https://forum.logik.tv/u/claussteinmassl)\
**Post date:** [August 11, 2021, 11:50am UTC](https://forum.logik.tv/t/mimic-os-walk/3631/2 "2021-08-11T11:50:49Z")

</div>

Recursion is your friend! 🙂  
By calling the function from itself again you can traverse through an unknown level of subfolders.

Here is a little example to build a list of all clips. I’ve modified some names slightly to stick to some Autodesk terminology (e.g. clips instead of entries etc.) to make things more understandable. Hope this helps. 🙂

```auto
import flame

clips = []

def build_clip_list(current_folder):
    """recursive search for entries"""
    global clips
    for folder in current_folder:
        if has_subfolder(folder):
            build_clip_list(folder.folders)

        for clip in folder.clips:
            clips.append(clip)
    

def collect_clips():
    """function that returns all clips as a list"""
    global clips
    clips = []

    for library in flame.project.current_project.current_workspace.libraries:
        build_clip_list(library.folders)

    return clips

def has_subfolder(folder):
    """check if the given folder has any subfolders"""
    if len(folder.folders) == 0:
        return False
    else:
        return True

print collect_clips()

```

This could be improved by running set on the list at the end or implementing another mechanism to avoid duplicate entries in the list.

---

<div class="post-metadata">

**Author:** ![kyleobley](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kyleobley](https://forum.logik.tv/u/kyleobley)\
**Post date:** [August 11, 2021, 12:25pm UTC](https://forum.logik.tv/t/mimic-os-walk/3631/3 "2021-08-11T12:25:32Z")

</div>

Oh man, so simple! Love it. I didn’t realize you could call a function within itself…

Thank you so much @claussteinmassl !

---

<div class="post-metadata">

**Author:** ![claussteinmassl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.logik.tv/claussteinmassl/32/1922_2.png) [@claussteinmassl](https://forum.logik.tv/u/claussteinmassl)\
**Post date:** [August 11, 2021, 12:39pm UTC](https://forum.logik.tv/t/mimic-os-walk/3631/4 "2021-08-11T12:39:10Z")

</div>

You’re welcome! 🙂

---

<div class="post-metadata">

**Author:** ![kyleobley](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kyleobley](https://forum.logik.tv/u/kyleobley)\
**Post date:** [August 11, 2021, 1:42pm UTC](https://forum.logik.tv/t/mimic-os-walk/3631/5 "2021-08-11T13:42:14Z")

</div>

Since we’re here…do you know if it’s possible to get the sequence range from a file path? Currently I can only seem to get `collected_clips[i].versions[0].tracks[0].segments[0].file_path` to return the file path of the first file.

I.e. that will currently return `somefile_0000.exr` and not `somefile_[0000-0120].exr` which is what .mio files hold as well as what’s shown in the media panel under clip information.

---

<div class="post-metadata">

**Author:** ![claussteinmassl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.logik.tv/claussteinmassl/32/1922_2.png) [@claussteinmassl](https://forum.logik.tv/u/claussteinmassl)\
**Post date:** [August 11, 2021, 3:06pm UTC](https://forum.logik.tv/t/mimic-os-walk/3631/6 "2021-08-11T15:06:59Z")

</div>

I don’t know of a direct implementation. Not in Flame and not in Python in general. However there are projects like this, which can be a great starting point if you don’t want to reinvent the wheel:

> **[Fileseq](https://pypi.org/project/Fileseq/)**
>
> A Python library for parsing frame ranges and file sequences commonly used in VFX and Animation applications.

Knowing the file path to the first frame, you could parse the directory to get the entire sequence.

---

<div class="post-metadata">

**Author:** ![kyleobley](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kyleobley](https://forum.logik.tv/u/kyleobley)\
**Post date:** [August 11, 2021, 3:26pm UTC](https://forum.logik.tv/t/mimic-os-walk/3631/7 "2021-08-11T15:26:18Z")

</div>

Oh nice one, thank you.

---

<div class="post-metadata">

**Author:** ![Stefan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.logik.tv/stefan/32/631_2.png) [@Stefan](https://forum.logik.tv/u/Stefan)\
**Post date:** [August 18, 2021, 11:48pm UTC](https://forum.logik.tv/t/mimic-os-walk/3631/8 "2021-08-18T23:48:27Z")

</div>

Cool, I didn’t know this one, thanks @claussteinmassl  
@ kyleobley you can also have a look at the clique module, specifically for image sequences :

> **[clique](https://pypi.org/project/clique/)**
>
> Manage collections with common numerical component

---

<div class="post-metadata">

**Author:** ![Stefan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.logik.tv/stefan/32/631_2.png) [@Stefan](https://forum.logik.tv/u/Stefan)\
**Post date:** [June 25, 2022, 6:10am UTC](https://forum.logik.tv/t/mimic-os-walk/3631/9 "2022-06-25T06:10:08Z")

</div>

Just used this for finding all sequences. Thanks a lot @claussteinmassl !!!
