Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created September 14, 2024 12:09
Show Gist options
  • Save me-suzy/d77cdae6d2dec9b41afb5baff1604668 to your computer and use it in GitHub Desktop.
Save me-suzy/d77cdae6d2dec9b41afb5baff1604668 to your computer and use it in GitHub Desktop.
gaseste binar ce extensie au fisierele
import os
import struct
def get_file_type(file_path):
with open(file_path, 'rb') as file:
header = file.read(4096) # Citim primii 4096 bytes pentru o analiză mai detaliată
# Verificăm semnături comune de fișiere
if header.startswith(b'\x89PNG\r\n\x1a\n'):
return 'image/png', '.png'
elif header.startswith(b'\xff\xd8\xff'):
return 'image/jpeg', '.jpg'
elif header.startswith(b'GIF87a') or header.startswith(b'GIF89a'):
return 'image/gif', '.gif'
elif header.startswith(b'%PDF'):
return 'application/pdf', '.pdf'
elif header.startswith(b'PK\x03\x04'):
return 'application/zip', '.zip'
elif header.startswith(b'\x1f\x8b\x08'):
return 'application/gzip', '.gz'
elif header[257:262] == b'ustar':
return 'application/x-tar', '.tar'
elif header.startswith(b'\x42\x4d'):
return 'image/bmp', '.bmp'
elif header.startswith(b'\x00\x00\x01\x00'):
return 'image/x-icon', '.ico'
# Verificare pentru fișiere EPUB (care sunt de fapt arhive ZIP)
if header.startswith(b'PK\x03\x04'):
try:
with open(file_path, 'rb') as f:
f.seek(30)
if f.read(8) == b'mimetype':
f.seek(38)
if f.read(20) == b'application/epub+zip':
return 'application/epub+zip', '.epub'
except:
pass # În caz că nu putem citi fișierul complet
# Dacă nu se potrivește cu niciuna dintre semnăturile cunoscute
return 'application/octet-stream', '.bin'
# Specificați calea către folderul dorit
folder_path = "e:/Carte/BB/17 - Site Leadership/alte/Ionel Balauta/Aryeht/Task 1 - Traduce tot site-ul/Doar Google Web/Andreea/Meditatii/2023/Chome/google-play-book-downloader-main/books/KOOEDwAAQBAJ"
# Obțineți lista tuturor fișierelor din folder
files = os.listdir(folder_path)
for file in files:
full_path = os.path.join(folder_path, file)
mime_type, suggested_extension = get_file_type(full_path)
print(f"Fișier: {file}")
print(f"Tip MIME detectat: {mime_type}")
print(f"Extensie sugerată: {suggested_extension}")
print("---")
# Întrebați utilizatorul dacă dorește să redenumească fișierul
answer = input(f"Doriți să redenumiti '{file}' cu extensia '{suggested_extension}'? (da/nu): ").lower()
if answer == 'da':
new_name = file + suggested_extension
new_path = os.path.join(folder_path, new_name)
os.rename(full_path, new_path)
print(f"Fișier redenumit în: {new_name}")
else:
print("Fișierul nu a fost redenumit.")
print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment