Last active
April 11, 2017 07:17
-
-
Save iamkissg/de444d5b9b654f38f28daee14c60b4fd to your computer and use it in GitHub Desktop.
Tookit for daily requirement
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
# | |
# A simple Toolkit for my daily requirement. | |
# | |
__author__ = "Engine" | |
__date__ = "2017-04-03" | |
import os | |
import random | |
import shutil | |
import string | |
import fire | |
class Toolkit(object): | |
"""A simple Toolkit for my daily requirement.""" | |
def random_string(self, length): | |
return ''.join(random.SystemRandom().choice(string.letters + string.digits) for _ in range(length)) | |
def reorganize(self, src, dst, ext, size=250): | |
""" | |
:param src: original directory | |
:param dst: destination | |
:param ext: file type | |
:param size: goal size | |
""" | |
if os.path.exists(dst): | |
os.makedirs(dst) | |
ls = [] | |
for item in os.listdir(src): | |
fn = os.path.join(src, item) | |
print(fn) | |
if os.path.isdir(fn): | |
self.reorganize(fn, dst, ext, size=250) | |
elif os.path.isfile(fn) and fn.lower().endswith(ext): | |
if len(ls) < size: | |
ls.append(fn) | |
else: | |
dirname = os.path.join(dst, self.random_string(16)) | |
os.makedirs(dirname) | |
for i in ls: | |
# shutil.copy(i, os.path.join(dirname, fn.split("/")[-1])) | |
shutil.copy(i, dirname) | |
del ls[:] | |
return True | |
def unify_extension(self, src, ext): | |
""" | |
:param src: directory | |
:param ext: file extension | |
""" | |
for item in os.listdir(src): | |
fn = os.path.join(src, item) | |
if os.path.isdir(fn): | |
self.unify_extension(fn, ext) | |
elif os.path.isfile(fn) and fn.lower().endswith(ext): | |
part1, part2 = os.path.splitext(fn) | |
os.rename(fn, part1 + part2.lower()) | |
return True | |
# todo: add more | |
if __name__ == "__main__": | |
fire.Fire(Toolkit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment