When a path ending with \ (on Windows) or / is given, some path operation, including string slicing and splitting, may produce wrong results after os.path.join. For example, if you join "D:\my_folder\" or "D:\my_folder" with "my_file.wav", the result is always "D:\my_folder\my_file.wav", and if you slice the result from len(my_folder) or len(my_folder) + 1 to get the filename, it can produce wrong results.
Solution
The solution is to migrate path operations completely to pathlib. The above operations can simply be:
import pathlib
my_folder = pathlib.Path("D:\my_folder\")
my_file = "my_file.wav"
full_path = my_folder / my_file # equivalent to os.path.join
file_name = full_path.name # will get "my_file.wav" correctly
Changes in this PR
This PR modifies the function traverse_dir in batch_infer.py. It is now migrated to pathlib and returns List[pathlib.Path] instead of List[str]. One of its argument is renamed and the docstring is added.
I noticed there are several other traverse_dirs in this repository. I highly recommend that they all be migrate to pathlib to reduce the risks when doing path operations.
Potential bugs
When a path ending with
\
(on Windows) or/
is given, some path operation, including string slicing and splitting, may produce wrong results afteros.path.join
. For example, if you join "D:\my_folder\" or "D:\my_folder" with "my_file.wav", the result is always "D:\my_folder\my_file.wav", and if you slice the result fromlen(my_folder)
orlen(my_folder) + 1
to get the filename, it can produce wrong results.Solution
The solution is to migrate path operations completely to
pathlib
. The above operations can simply be:Changes in this PR
This PR modifies the function
traverse_dir
inbatch_infer.py
. It is now migrated topathlib
and returnsList[pathlib.Path]
instead ofList[str]
. One of its argument is renamed and the docstring is added.I noticed there are several other
traverse_dir
s in this repository. I highly recommend that they all be migrate topathlib
to reduce the risks when doing path operations.