./mydata/HyperSim/extracted ├── ai001001 │ ├──… · The Chat Archive
Explore ./mydata/HyperSim/extracted ├── ai001001 │ ├── dir1file1.txt │ ├── dir1file2.txt │ ├──… Save Share Download ▾ Report./mydata/HyperSim/extracted
├── ai_001_001
│ ├── dir_1_file_1.txt
│ ├── dir_1_file_2.txt
│ ├── dir_1_file_3.txt
│ └── sub_dir_1
│ └── sub_file_1.txt
├── dir_2
│ └── dir_2_file_1.txt
├── dir_3
└── file_1.txt
Assistant2024-12-02 08:41 UTC Copy response
It looks like you've provided a directory structure for a folder named extracted located in ./mydata/HyperSim/. Here's a breakdown of the structure:
extracted
├── ai_001_001
│ ├── dir_1_file_1.txt
│ ├── dir_1_file_2.txt
│ ├── dir_1_file_3.txt
│ └── sub_dir_1
│ └── sub_file_1.txt
├── dir_2
│ └── dir_2_file_1.txt
├── dir_3
└── file_1.txt
Components of the Structure:
ai_001_001 : This appears to be a directory that contains several text files and a subdirectory:
dir_1_file_1.txt
dir_1_file_2.txt
dir_1_file_3.txt
sub_dir_1 : This is a subdirectory that contains:
dir_2 : This is another directory containing one text file:
dir_3 : A directory listed here, but it currently contains no files.
import os
import glob
目标文件夹路径
base_folder = '/home/notebook/data/group/dzb/mydata/HyperSim/extracted/ai_001_001/00/depth'
Assistant2024-12-02 08:48 UTC Copy response
您想要遍历指定的文件夹结构,查找所有后缀为 .depth_meters.png 的深度文件和相应的图像文件,并将它们的路径写入到一个输出文件中。以下是对您现有代码的稍微调整,以正确处理整个文件夹结构并保证准确匹配深度文件和图像文件。
Copy first prompt Copy first response
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 2 (4 messages)
Language en
Conversation date Dec 2, 2024
Added to archive Nov 16, 2026
ID 01m7h42ajh09gbpf0170gqqdk9Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 3002eac74fe6164103bfe5c433dd1737
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
file_1.txt : A standalone file located directly in the extracted directory.
Summary The directory contains a combination of files and subdirectories with a variety of text files. If you need assistance with specific tasks related to these files or how to manage them, feel free to ask!
输出文件路径 output_file = '/home/notebook/data/group/dzb/mask_patch/hypersim.txt'
def find_and_write_pairs(base_folder, output_file):
# 打开输出文件
with open(output_file, 'w') as f_out:
# 遍历所有子目录
for root, dirs, files in os.walk(base_folder):
depth_files = {}
image_files = {}
# 归类文件
for file in files:
if file.endswith('.depth_meters.png'):
# 获取basename(去掉后缀)
basename = file.rsplit('.', 3)[0] # frame.0002.depth_meters
if basename not in depth_files:
depth_files[basename] = []
depth_files[basename].append(os.path.join(root, file))
elif file.endswith('.color.jpg'):
# 获取basename(去掉后缀)
basename = file.rsplit('.', 2)[0] # frame.0000.color
if basename not in image_files:
image_files[basename] = []
image_files[basename].append(os.path.join(root, file))
# 匹配深度和图像文件
for basename, depth_paths in depth_files.items():
if basename in image_files: # 如果图像文件也存在
# 取第一个文件(如果有多个可以根据需求选取)
for depth_path in depth_paths:
for image_path in image_files[basename]:
f_out.write(f"{depth_path} | {image_path}\n")
if name == 'main ':
find_and_write_pairs(base_folder, output_file)
./mydata/HyperSim/extracted
├── ai_001_001
│ ├── 00
│ │ ├──depth
│ │ │ ├──frame.0000.depth_meters.png
│ │ │ ├──frame.0001.depth_meters.png
│ │ │ ├──……
│ │ │ ├──frame.0099.depth_meters.png
│ │ ├──image
│ │ │ ├──frame.0000.color.jpg
│ │ │ ├──frame.0001.color.jpg
│ │ │ ├──……
│ │ │ ├──frame.0099.color.jpg
├── ai_001_002
│ └── 00
│ │ ├──depth
│ │ │ ├──frame.0000.depth_meters.png
│ │ │ ├──frame.0001.depth_meters.png
│ │ │ ├──……
│ │ │ ├──frame.0099.depth_meters.png
│ │ ├──image
│ │ │ ├──frame.0000.color.jpg
│ │ │ ├──frame.0001.color.jpg
│ │ │ ├──……
│ │ │ ├──frame.0099.color.jpg
│ └── 01
│ │ ├──depth
│ │ ├──image
│ └── 02
│ │ ├──depth
│ │ ├──image
│ └── 03
│ │ ├──depth
│ │ ├──image
├── ai_001_003
│ └── 00
│ │ ├──depth
│ │ ├──image
……
├── ai_055_010
│ └── 01
│ │ ├──depth
│ │ │ ├──frame.0000.depth_meters.png
│ │ │ ├──frame.0001.depth_meters.png
│ │ │ ├──……
│ │ │ ├──frame.0099.depth_meters.png
│ │ ├──image
│ │ │ ├──frame.0000.color.jpg
│ │ │ ├──frame.0001.color.jpg
│ │ │ ├──……
│ │ │ ├──frame.0099.color.jpg
文件夹结构如上,需要遍历所有子文件夹中含后缀为.depth_meters.png的文件,并将其输入到一个txt中
import os
# 基本文件夹路径
base_folder = '/home/notebook/data/group/dzb/mydata/HyperSim/extracted/'
# 输出文件路径
output_file = '/home/notebook/data/group/dzb/mask_patch/hypersim.txt'
def find_and_write_pairs(base_folder, output_file):
# 打开输出文件
with open(output_file, 'w') as f_out:
# 遍历所有子目录
for ai_folder in os.listdir(base_folder):
ai_path = os.path.join(base_folder, ai_folder)
if os.path.isdir(ai_path): # 仅处理目录
for root, dirs, files in os.walk(ai_path):
depth_files = {}
image_files = {}
# 归类文件
for file in files:
if file.endswith('.depth_meters.png'):
# 获取basename(去掉后缀)
basename = file.rsplit('.', 3)[0] # frame.0000.depth_meters
depth_path = os.path.join(root, file)
depth_files[basename] = depth_path # 储存路径
elif file.endswith('.color.jpg'):
# 获取basename(去掉后缀)
basename = file.rsplit('.', 2)[0] # frame.0000.color
image_path = os.path.join(root, file)
image_files[basename] = image_path # 储存路径
# 匹配深度和图像文件
for basename, depth_path in depth_files.items():
if basename in image_files: # 如果相应的图像文件也存在
image_path = image_files[basename]
f_out.write(f"{depth_path} | {image_path}\n")
if __name__ == '__main__':
find_and_write_pairs(base_folder, output_file)
代码说明:
遍历目录结构 : 代码首先列出 base_folder 下的所有内容,并检查每一个项目是否是目录(即 ai_001_001, ai_001_002 等)。
分类文件 : 在每个 ai_* 目录下,它会遍历所有子目录,找出后缀为 .depth_meters.png 和 .color.jpg 的文件,并将它们的路径存储在字典中。
匹配文件 : 对于每一个深度文件,根据其 basename(去掉后缀部分)去查找相应的图像文件。如果找到匹配项,就将它们的路径按照格式写入输出文件。
输出结果 : 结果会按照 “深度文件路径 | 图像文件路径” 的形式输出至 /home/notebook/data/group/dzb/mask_patch/hypersim.txt 文件中。
注意事项:
确保文件夹路径和文件类型符合您实际需要的内容。
使用 os.walk 可以递归地找到所有子目录,而 os.listdir 让我们能够首先获取每个 ai_* 目录。
如果目录中包含大量文件,确保输出文件不超出文件系统的大小限制。
您可以直接运行此代码进行处理。如有需要,可以对路径或文件名的匹配逻辑进行微调。
Not yet included in a published release.