Skip to content

Can't move files after opening them #349

Open

Description

@firas5673

I can't move game files after loading them with UnityPy.
I call a function that has env = UnityPy.load(file_path) inside it, then after that when I try to move the file to another location, I can't do that.

Here's a simplified version of the code:

The function:

def is_excluded_file(file_path):
    try:
        env = UnityPy.load(file_path)
    except:
        return False
    for path,obj in env.container.items():
        if condition(path):
            return True
    return False

for file_path in file_walker(assetbundle_path): #recursively yields every file's path in the folder
    if is_excluded_file(file_path):
        dir_path  = os.path.dirname(file_path)
        nested_path_lib = Path(os.path.join(move_folder, dir_path))
        nested_path_lib.mkdir(parents=True, exist_ok=True)
        shutil.move(file_path, os.path.join(move_folder, file_path))

I get an error at that last line, saying that it's being used by another process (which is UnityPy).
Is there a way to "close" this process?

Activity

  1. isHarryh commented on Nov 2, 2025

    @isHarryh
    Contributor

    See #332 .

  2. isHarryh commented on Nov 2, 2025

    @isHarryh
    Contributor

    For a temporary solution, you can "record" the files you want move to a txt file, then run another script to move them.

  3. firas5673 commented on Nov 2, 2025

    @firas5673
    Author

    For a temporary solution, you can "record" the files you want move to a txt file, then run another script to move them.

    Yeah, I did exactly that. My project is small, but I could see this becoming an issue in bigger projects.

  4. K0lb3 commented on Nov 3, 2025

    @K0lb3
    Owner

    UnityPy doesn't explicitly call close on opened handles.
    It just drops the object, so the garbage cleaner will at some point close the file handle.

    You basically have three ways to work around this.

    1. pass file handles to UnityPy.load instead of paths, and close them yourself
    2. force a gc cleaner pass (rather expensive) after you're done with the env
    3. iterate over the env content and close all streams (bit more tricky)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions