-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update track.py (fix "bad escape \M") #52
base: main
Are you sure you want to change the base?
Conversation
- added a sanitize_string function to sanitize strings for filenames or directories - used this function to sanitize strings before constructing file paths - modify the regular expression to correctly match filenames that start with the sanitized filename - changed the file renaming process to use shutil.move instead of Path(filename_temp).rename(filename) to handle potential issues when the source and destination are in the same directory (files being renamed to "._1") needs to be tested more, but it fixes the problem.
I know you said it still needs testing but I hit an album where I can consistently reproduce and I get the error with this one even with your patch.
|
if not check_id and check_name:
# Convert filename to a string before escaping
filename_str = str(PurePath(filename))
pattern = re.escape(filename_str) + '_'
c = len([file for file in Path(filedir).iterdir() if re.search(f'^{pattern}', str(file))]) + 1
fname = PurePath(filename).stem
ext = PurePath(filename).suffix
# Use the filename_str for pattern matching and then construct the new filename with the original Path object
filename = PurePath(filedir).joinpath(f'{fname}_{c}{ext}') Won't this be enough to fix it? It works for me hahaha |
This was my fix, got the line count down and haven't had any naming issues since. Didn't do any sanitization though. if not check_id and check_name:
c = len([file for file in Path(filedir).iterdir() if file.match(filename.stem + "*")])
filename = PurePath(filedir).joinpath(f'{filename.stem}_{c}{filename.suffix}') Got around having to use shutil when renaming temps this way. if filename_temp != filename:
if Path(filename).exists():
Path(filename).unlink()
Path(filename_temp).rename(filename) Both implemented on my fork and works well enough for me. I have not tested it extensively, though. I will probably come back to this. |
There is already a function for sanitising strings for file names. Lines 245 to 266 in fa2156b
|
needs to be tested more, but it fixes the problem from the title.