[Python] How to construct an in-memory virtual file system and then write this structure to...

Discussão em 'Python' iniciado por Stack, Agosto 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking for a way to create a virtual file system in Python for creating directories and files, before writing these directories and files to disk.

    Using PyFilesystem I can construct a memory filesystem using the following:

    >>> import fs
    >>> temp_dir = fs.open_fs('mem://')
    >>> temp_dir.makedirs('fruit')
    SubFS(MemoryFS(), '/fruit')
    >>> temp_dir.makedirs('vegetables')
    SubFS(MemoryFS(), '/vegetables')
    >>> with temp_dir.open('fruit/apple.txt', 'w') as apple: apple.write('braeburn')
    ...
    8
    >>> temp_dir.tree()
    ├── fruit
    │ └── apple.txt
    └── vegetables


    Ideally, I want to be able to do something like:

    temp_dir.write_to_disk('<base path>')


    To write this structure to disk, where <base path> is the parent directory in which this structure will be created.

    As far as I can tell, PyFilesystem has no way of achieving this. Is there anything else I could use instead or would I have to implement this myself?

    Continue reading...

Compartilhe esta Página