是否可以在本地电脑上创建与文件路径匹配的HDF5结构



我正在尝试使用本地电脑上的文件路径自动创建HDF5结构。我想读取子目录并创建一个HDF5结构以匹配,然后我可以将文件保存到。谢谢

您可以通过组合os.walk()和h5py-create_group()来实现这一点。唯一复杂的是处理Linux与Windows(以及Windows上的驱动器号(。另一个考虑因素是相对路径与绝对路径。(我使用了绝对路径,但我的示例可以修改。(注意:它有点冗长,所以你可以看到发生了什么。(以下是示例代码(适用于Windows(:

with h5py.File('SO_73879694.h5','w') as h5f:
cwd = os.getcwd()
for root, dirs, _ in os.walk(cwd, topdown=True):
print(f'ROOT: {root}')
# for Windows, modify root: remove drive letter and replace backslashes:
grp_name = root[2:].replace( '\', '/')
print(f'grp_name: {grp_name}n')
h5f.create_group(grp_name)

在Python中使用HDFql实际上很容易做到这一点。这里有一个完整的脚本可以做到这一点:

# import HDFql module (make sure it can be found by the Python interpreter)
import HDFql
# create and use (i.e. open) an HDF5 file named 'my_file.h5'
HDFql.execute("CREATE AND USE FILE my_file.h5")
# get all directories recursively starting from the root of the file system (/)
HDFql.execute("SHOW DIRECTORY / LIKE **")
# iterate result set and create group in each iteration
while HDFql.cursor_next() == HDFql.SUCCESS:
HDFql.execute("CREATE GROUP "%s"" % HDFql.cursor_get_char())

最新更新