Elron commited on
Commit
7dd1400
1 Parent(s): 33a5854

Upload file_utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. file_utils.py +27 -0
file_utils.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+
4
+ def get_all_files_in_dir(dir_path: str, recursive: bool = False, file_extension: str = None):
5
+ """
6
+ Get all files in a directory. Optionally recursively.
7
+ Optionally filter by file extension.
8
+
9
+ :param dir_path: The directory path to search for files.
10
+ :param recursive: Whether to search recursively in subdirectories.
11
+ :param file_extension: The file extension to filter by (e.g., '.txt').
12
+ :return: A list of file paths.
13
+ """
14
+ if not os.path.isdir(dir_path):
15
+ raise ValueError(f"{dir_path} is not a directory")
16
+
17
+ files = []
18
+ for root, _, filenames in os.walk(dir_path):
19
+ for filename in filenames:
20
+ if file_extension:
21
+ if filename.endswith(file_extension):
22
+ files.append(os.path.join(root, filename))
23
+ else:
24
+ files.append(os.path.join(root, filename))
25
+ if not recursive:
26
+ break
27
+ return files