|
29 | 29 |
|
30 | 30 | #################################################################################################### |
31 | 31 |
|
32 | | -def find(file_name, directories): |
| 32 | +def find(file_name: str, directories: list[str]) -> Path: |
33 | 33 | # Fixme: bytes ??? |
34 | | - if isinstance(directories, bytes): |
35 | | - directories = (directories,) |
| 34 | + # on Linux path are bytes, thus some files can be invalid utf8... |
| 35 | + # if isinstance(directories, bytes): |
| 36 | + # directories = (directories,) |
36 | 37 | for directory in directories: |
37 | | - for directory_path, _, file_names in os.walk(directory): |
| 38 | + directory = Path(directory) |
| 39 | + # for directory_path, _, file_names in os.walk(directory): |
| 40 | + for directory_path, _, file_names in directory.walk(): |
| 41 | + directory_path = Path(directory_path) |
38 | 42 | if file_name in file_names: |
39 | | - return os.path.join(directory_path, file_name) |
40 | | - raise NameError("File %s not found in directories %s" % (file_name, str(directories))) |
| 43 | + return directory_path.joinpath(file_name) |
| 44 | + raise NameError(f"File {file_name} not found in directories {directories}") |
41 | 45 |
|
42 | 46 | #################################################################################################### |
43 | 47 |
|
44 | | -def expand_path(path) -> Path: |
| 48 | +def expand_path(path: Path | str) -> Path: |
| 49 | + # Substrings of the form $name or ${name} are replaced by the value of environment variable name. |
| 50 | + # On Unix and Windows, return the argument with an initial component of ~ or ~user |
| 51 | + # replaced by that user’s home directory. |
45 | 52 | _ = os.path.expandvars(path) |
46 | 53 | return Path(_).expanduser().absolute() |
47 | 54 |
|
|
0 commit comments