ASTERIZER commited on
Commit
616615e
Β·
1 Parent(s): 486816d

Fix: flatten deeply nested zip extracts (data/litdata_pretrain_final/)

Browse files
Files changed (1) hide show
  1. fetch_data.py +11 -4
fetch_data.py CHANGED
@@ -117,15 +117,15 @@ def _extract_zips(data_dir: Path):
117
 
118
 
119
  def _flatten_to_root(data_dir: Path):
120
- """If index.json is one level deep (e.g. data_dir/subfolder/index.json),
121
  move everything from that subfolder up to data_dir."""
122
  if (data_dir / "index.json").exists():
123
  return # already at root
124
- candidates = list(data_dir.glob("*/index.json"))
125
  if len(candidates) != 1:
126
  return # ambiguous or not found
127
  sub = candidates[0].parent
128
- print(f" Moving contents from {sub.name}/ up to {data_dir.name}/ ...")
129
  for item in sub.iterdir():
130
  dest = data_dir / item.name
131
  if dest.exists():
@@ -134,7 +134,14 @@ def _flatten_to_root(data_dir: Path):
134
  else:
135
  dest.unlink()
136
  shutil.move(str(item), str(dest))
137
- sub.rmdir()
 
 
 
 
 
 
 
138
 
139
 
140
  # ─── Verify ───────────────────────────────────────────────────────────────────
 
117
 
118
 
119
  def _flatten_to_root(data_dir: Path):
120
+ """If index.json is nested (e.g. data_dir/a/b/index.json),
121
  move everything from that subfolder up to data_dir."""
122
  if (data_dir / "index.json").exists():
123
  return # already at root
124
+ candidates = list(data_dir.glob("**/index.json"))
125
  if len(candidates) != 1:
126
  return # ambiguous or not found
127
  sub = candidates[0].parent
128
+ print(f" Moving contents from {sub.relative_to(data_dir)}/ up to {data_dir.name}/ ...")
129
  for item in sub.iterdir():
130
  dest = data_dir / item.name
131
  if dest.exists():
 
134
  else:
135
  dest.unlink()
136
  shutil.move(str(item), str(dest))
137
+ # Remove the now-empty nested directories
138
+ # Walk up from sub to data_dir, removing empty dirs
139
+ while sub != data_dir:
140
+ try:
141
+ sub.rmdir()
142
+ except OSError:
143
+ break
144
+ sub = sub.parent
145
 
146
 
147
  # ─── Verify ───────────────────────────────────────────────────────────────────