Last active
April 22, 2017 11:54
-
-
Save wzyboy/d6ce3ce7aa4ebc6b9b4565f55926fa8c to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import os | |
import sys | |
from zlib import crc32 | |
def crc(filename): | |
MAX_FILE_SIZE = 1024 * 1024 * 1024 * 2 # 2 GiB | |
if os.path.getsize(filename) > MAX_FILE_SIZE: | |
return NotImplemented | |
data = open(filename, 'rb').read() | |
c = crc32(data) | |
return '{:08X}'.format(c) | |
def main(filename): | |
dirname = os.path.dirname(filename) | |
basename = os.path.basename(filename) | |
name, ext = os.path.splitext(basename) | |
new_name = crc(filename) | |
new_ext = ext.lower() | |
new_basename = '{}{}'.format(new_name, new_ext) | |
new_filename = os.path.join(dirname, new_basename) | |
print('{} \t-> {}'.format(filename, new_filename)) | |
os.rename(filename, new_filename) | |
if __name__ == '__main__': | |
filenames = sys.argv[1:] | |
if not filenames: | |
raise SystemExit('Usage: {} FILE [FILE]...'.format(__file__)) | |
for filename in filenames: | |
main(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment