Python中是否有允许在单个文件中管理虚拟文件系统的库?

2024-09-30 01:32:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个节目。我不认为我需要在这里展示它,但我想知道是否有可能创建存储在单个文件中的虚拟文件系统。例如,我有一个名为my_file_system.fs的文件,有没有办法只将虚拟文件系统创建到单个文件中。基本上:

/home/xcodz/
    |
    +--myfilesystem.fs
       |
       +--testdir
       +--test.txt
       +--downloads
          |
          +--example1.txt

我基本上想要基本的文件系统接口。没有所有者、日期或其他元数据。Zip是一个好主意,但它只是一次读取系统中的整个文件,不提供类似文件的接口。因此,我需要一个非常基本的单文件文件文件系统,在这个系统中,我可以像普通IO对象一样使用文件

编辑 存储在文件系统中的文件对于单个文件将有3GB的大小,而我没有那么多的ram。TarFiles似乎并没有让我的工作变得更好

编辑 我的意思是说一些文件系统,就像有虚拟盒的文件系统一样


Tags: 文件testtxt编辑homemy系统fs
2条回答

您可以使用SVFS

SVFS allows to create virtual filesystem inside file on real filesystem. It can be used to store multiple files inside single file (with directory structure). Unlike archives, SVFS allows to modify files in-place. SVFS files use file-like interface, so they can be used (pretty much) like regular Python file objects. Finally, it’s implemented in pure python and doesn’t use any 3rd party modules, so it should be very portable. Tests show write speed to be around 10-12 MB/s and read speed to be around 26-28 MB/s.

解决方案#1-TAR文件

TAR文件基本上是单个文件中的unix文件系统。您可以使用tarfile在python中使用它们

优点:

缺点:

  • 没有非POSIX功能,如加密或内存映射文件
  • 文件无法就地编辑,您必须先提取它们,然后重新添加它们

解决方案#2-环回文件系统

如果为了运行您的程序,您可以要求完成装载,您可以use a loopback filesystem

$ truncate -s 100M /tmp/loopback.ext4
$ mkfs -t ext4 /tmp/loopback.ext4
mke2fs 1.45.5 (07-Jan-2020)
Discarding device blocks: done                            
Creating filesystem with 25600 4k blocks and 25600 inodes

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

$ sudo mkdir /mnt/loop
$ sudo mount -o loop /tmp/loopback.ext4 /mnt/loop/
$ df -T /mnt/loop
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/loop11    ext4   93M   72K   86M   1% /mnt/loop
$ sudo tree /mnt/loop/
/mnt/loop/
└── lost+found

1 directory, 0 files

优点:

  • 像常规文件系统一样使用
  • 可从python进程外部离线和在线访问
  • 很容易调试
  • 您可以添加加密、使用内存映射文件以及真实文件系统的任何其他功能

缺点:

  • 需要根
  • 在运行进程之前需要装载
  • 需要卸载(至少在崩溃的情况下)
  • 必须预先设置大小,调整大小是可能的,但不是微不足道的
  • Very difficult to support cross-platform

解决方案#3-DYI文件系统

因为您最关心文件I/O,所以可以使用BytesIO实现它。要支持文件系统层次结构中的多个文件,可以将这些文件放在trie中。您需要序列化和反序列化所有这些,您可以使用pickle

优点:

  • 比基于TAR的解决方案更易于定制
  • 可以做成一个图书馆,很好,可以重复使用

缺点:

  • 你需要更多的编码
  • 每次都对整个数据结构进行酸洗是不可伸缩的
  • 如果需要碰撞安全,则需要在每次(相关)修改trie或任何文件后进行pickle

选择什么

因为您的需求非常基本,所以选择1-TAR文件

相关问题 更多 >

    热门问题