File size: 2,096 Bytes
6ef117e
 
 
e34b08b
9d91797
6ef117e
 
 
 
 
 
9d91797
06b2c5c
9d91797
e34b08b
9d91797
e34b08b
9d91797
 
e34b08b
9d91797
 
e34b08b
9d91797
 
e34b08b
9d91797
e34b08b
 
 
 
 
 
 
 
 
 
 
 
 
47fbc1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e34b08b
9d91797
e34b08b
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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}")
    print("Temp files cleaned up. Goodbye")

def rename_file_to_lowercase_extension(file_path: str) -> str:
    """
    Renames a file's extension to lowercase in place.

    Parameters:
        file_path (str): The original file path.

    Returns:
        str: The new file path with the lowercase extension.

    Raises:
        OSError: If there is an error renaming the file (e.g., file not found, permissions issue).
    """
    # Split the path into directory and filename
    directory, filename = os.path.split(file_path)
    
    # Split the filename into name and extension
    name, ext = os.path.splitext(filename)
    
    # Convert the extension to lowercase
    new_ext = ext.lower()
    
    # If the extension changes, rename the file
    if ext != new_ext:
        new_filename = name + new_ext
        new_file_path = os.path.join(directory, new_filename)
        try:
            os.rename(file_path, new_file_path)
            print(f"Rename {file_path} to {new_file_path}\n")
        except Exception as e:
            print(f"os.rename failed: {e}. Falling back to binary copy operation.")
            try:
                # Read the file in binary mode and write it to new_file_path
                with open(file_path, 'rb') as f:
                    data = f.read()
                with open(new_file_path, 'wb') as f:
                    f.write(data)
                    print(f"Rename {file_path} to {new_file_path}\n")
                # Optionally, remove the original file after copying
                #os.remove(file_path)
            except Exception as inner_e:
                print(f"Failed to copy file from {file_path} to {new_file_path}: {inner_e}")
                raise inner_e
        return new_file_path
    else:
        return file_path