Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,320 Bytes
6ef117e 9d91797 6ef117e 9d91797 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# file_utils
import os
import utils.constants as constants
import shutil
from pathlib import Path
def cleanup_temp_files():
for file_path in constants.temp_files:
try:
os.remove(file_path)
except Exception as e:
print(f"Failed to delete temp file {file_path}: {e}")
def rename_file_to_lowercase_extension(image_path: str) -> str:
"""
Renames only the file extension to lowercase by copying it to the temporary folder.
Parameters:
image_path (str): The original file path.
Returns:
str: The new file path in the temporary folder with the lowercase extension.
Raises:
Exception: If there is an error copying the file.
"""
path = Path(image_path)
new_suffix = path.suffix.lower()
new_path = path.with_suffix(new_suffix)
# Make a copy in the temporary folder with the lowercase extension
if path.suffix != new_suffix:
try:
shutil.copy(path, new_path)
constants.temp_files.append(str(new_path))
return str(new_path)
except FileExistsError:
raise Exception(f"Cannot copy {path} to {new_path}: target file already exists.")
except Exception as e:
raise Exception(f"Error copying file: {e}")
else:
return str(path) |