yxlllc / DDSP-SVC

Real-time end-to-end singing voice conversion system based on DDSP (Differentiable Digital Signal Processing)
MIT License
1.87k stars 250 forks source link

Migrate some path operation to pathlib to fix potential bugs #84

Closed yqzhishen closed 1 year ago

yqzhishen commented 1 year ago

Potential bugs

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.