-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_files.py
54 lines (43 loc) · 2.2 KB
/
rename_files.py
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
import os
import magic
import shutil
import mimetypes
def identify_file_type(file_path):
try:
file_type = magic.from_file(file_path, mime=True)
except Exception as e:
print(f"Error identifying file type for {file_path}: {e}")
return None
return file_type
def identify_files_in_directory(directory_path):
print("\n")
print("#" * 85) # Separator line
print("📂 Scanning directory:", directory_path)
print("-" * 85) # Separator line
# List files in the current directory only, excluding subdirectories
files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
for file in files:
file_path = os.path.join(directory_path, file)
file_type = identify_file_type(file_path)
# Get the current file extension
file_base_name, file_current_ext = os.path.splitext(file)
# Check if the current extension matches the identified extension
if file_current_ext:
print(f'📄 File: {file}\n 🗂️ Type: {file_type}\n ✅ Already has an extension: {file_current_ext}\n')
else:
# Guess the file extension based on the MIME type
file_ext = mimetypes.guess_extension(file_type)
# If guess_extension returns None, fallback to '.dat' extension
if not file_ext:
file_ext = '.dat'
new_file_name = f"{file_base_name}{file_ext}"
new_file_path = os.path.join(directory_path, new_file_name)
# Rename the file only if the new name is different from the current name
if new_file_path != file_path:
shutil.move(file_path, new_file_path) # Use shutil.move for renaming
print(f'📄 File: {file}\n 🗂️ Type: {file_type}\n ❌ Missing extension!\n ✅ Renamed to: {new_file_name}\n')
print("=" * 85) # Separator line
print("✅ Scan and correction complete.")
# Get the directory path where the script is located
directory_path = os.path.dirname(os.path.abspath(__file__))
identify_files_in_directory(directory_path)