Created
January 9, 2019 10:25
-
-
Save wodim/9464ad8c90072cecd8a40a625b5b97b3 to your computer and use it in GitHub Desktop.
Adds a suitable extension to every file in the current folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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