Finds all non-UTF-8 file paths in a given directory and returns their relative paths. Args: path (str): The path to the directory. Returns: list: A list of relative paths of non-UTF-8 files.
(path)
| 307 | return string |
| 308 | |
| 309 | def find_non_utf8_files(path): |
| 310 | """ |
| 311 | Finds all non-UTF-8 file paths in a given directory and returns their relative paths. |
| 312 | |
| 313 | Args: |
| 314 | path (str): The path to the directory. |
| 315 | |
| 316 | Returns: |
| 317 | list: A list of relative paths of non-UTF-8 files. |
| 318 | """ |
| 319 | non_utf8_files = [] |
| 320 | for root, dirs, files in os.walk(path): |
| 321 | for file in files: |
| 322 | file_path = os.path.join(root, file) |
| 323 | try: |
| 324 | with codecs.open(file_path, 'r', 'utf-8') as f: |
| 325 | f.read() |
| 326 | except UnicodeDecodeError: |
| 327 | # Calculate the relative path by removing the base directory path |
| 328 | relative_path = os.path.relpath(file_path, path) |
| 329 | non_utf8_files.append(relative_path) |
| 330 | return non_utf8_files |
| 331 | |
| 332 | def find_abs_path(folder, file_name): |
| 333 | # Walk through the directory and its subdirectories |