Skip to content

Instantly share code, notes, and snippets.

@wodim
Created January 9, 2019 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wodim/9464ad8c90072cecd8a40a625b5b97b3 to your computer and use it in GitHub Desktop.
Save wodim/9464ad8c90072cecd8a40a625b5b97b3 to your computer and use it in GitHub Desktop.
Adds a suitable extension to every file in the current folder
import mimetypes
import os
import sys
import magic
try:
dir = sys.argv[1]
except IndexError:
dir = os.getcwd()
files = [f for f in os.listdir(dir)
if os.path.isfile(os.path.join(dir, f))]
for file in files:
if file == sys.argv[0]:
continue
mimetype = magic.from_file(file, mime=True)
extension = mimetypes.guess_extension(mimetype)
if extension and not file.endswith(extension):
os.rename(file, file + extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment