我正在寻找一种方法,在将这些目录和文件写入磁盘之前,用Python创建一个虚拟文件系统来创建目录和文件。在
使用PyFilesystem我可以使用以下方法构造内存文件系统:
>>> import fs
>>> dir = fs.open_fs('mem://')
>>> dir.makedirs('fruit')
SubFS(MemoryFS(), '/fruit')
>>> dir.makedirs('vegetables')
SubFS(MemoryFS(), '/vegetables')
>>> with dir.open('fruit/apple.txt', 'w') as apple: apple.write('braeburn')
...
8
>>> dir.tree()
├── fruit
│ └── apple.txt
└── vegetables
理想情况下,我希望能够做一些类似的事情:
^{pr2}$要将此结构写入磁盘,其中<base path>
是将在其中创建此结构的父目录。在
据我所知,PyFilesystem无法实现这一点。还有什么可以替代的吗?或者我必须自己去实现吗?在
如果您只想在内存中暂存一个文件系统树,请查看(
tarfile
模块)[https://docs.python.org/3/library/tarfile.html]。在创建文件和目录有点复杂:
但是,您可以使用
^{pr2}$TarFile.extractall
创建文件系统层次结构:您可以使用^{} 从一个文件系统复制到另一个文件系统,或者使用^{} 来移动文件系统。在
考虑到PyFilesystem还抽象了底层系统文件系统^{} -事实上,这是默认协议,您只需将内存中的文件系统(^{} )复制到该文件系统,实际上,您将把它写入磁盘:
相关问题 更多 >
编程相关推荐