不久前,我实现了一个小型 python 库(https://github.com/kajic/vdir),用于创建虚拟目录、文件,甚至在需要时压缩它们。从自述文件(虚拟目录最后压缩):
from vdir import VDir
vd = VDir()
# Write to file
vd.open("path/to/some/file").write("your data")
# Create directory, go inside it, and write to some other file
vd.mkdir("foo")
vd.cd("foo")
vd.open("bar").write("something else") # writes to /foo/bar
# Read from file
vd.open("bar").read()
# Get the current path
vd.pwd()
# Copy directory and all its contents
vd.cp("/foo", "/foo_copy")
# Move the copied directory somewhere else
vd.mv("/foo_copy", "/foo_moved")
# Create a file, then remove it
vd.open("unnecessary").write("foo")
vd.rm("unnecessary")
# Walk over all directories and files in the virtual directory
vd.cd("/")
for base, dirnames, dirs, filenames, files in vd.walk():
pass
# Recursively list directory contents
vd.ls()
# Create a zip from the virtual directory
zip = vd.compress()
# Get zip data
zip.read()
我只是为了好玩而做的,并没有对它进行广泛的测试,但无论如何它可能对你有用。