MCPcopy Index your code
hub / github.com/going-doer/Paper2Code / read_all_files

Function read_all_files

codes/utils.py:365–409  ·  view source on GitHub ↗

Recursively read all .py files in the specified directory and return their contents.

(directory, allowed_ext, is_print=True)

Source from the content-addressed store, hash-verified

363
364
365def read_all_files(directory, allowed_ext, is_print=True):
366 """Recursively read all .py files in the specified directory and return their contents."""
367 all_files_content = {}
368
369 for root, _, files in os.walk(directory): # Recursively traverse directories
370 for filename in files:
371 relative_path = os.path.relpath(os.path.join(root, filename), directory) # Preserve directory structure
372
373 # print(f"fn: {filename}\tdirectory: {directory}")
374 _file_name, ext = os.path.splitext(filename)
375
376 is_skip = False
377 if len(directory) < len(root):
378 root2 = root[len(directory)+1:]
379 for dirname in root2.split("/"):
380 if dirname.startswith("."):
381 is_skip = True
382 break
383
384 if filename.startswith(".") or "requirements.txt" in filename or ext == "" or is_skip:
385 if is_print and ext == "":
386 print(f"[SKIP] {os.path.join(root, filename)}")
387 continue
388
389 if ext not in allowed_ext:
390 if _file_name.lower() != "readme":
391 if is_print:
392 print(f"[SKIP] {os.path.join(root, filename)}")
393 continue
394
395 try:
396 filepath = os.path.join(root, filename)
397 file_size = os.path.getsize(filepath) # bytes
398
399 if file_size > 204800: # > 200KB
400 print(f"[BIG] {filepath} {file_size}")
401
402 with open(filepath, "r") as file: # encoding="utf-8"
403 all_files_content[relative_path] = file.read()
404 except Exception as e:
405 print(e)
406 print(f"[SKIP] {os.path.join(root, filename)}")
407
408
409 return all_files_content
410
411def read_python_files(directory):
412 """Recursively read all .py files in the specified directory and return their contents."""

Callers 1

mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected