Skip to content

Instantly share code, notes, and snippets.

@takase1121
Last active November 11, 2020 01:59
Show Gist options
  • Save takase1121/e7513eaae767c031eeae2223f7b0ee5a to your computer and use it in GitHub Desktop.
Save takase1121/e7513eaae767c031eeae2223f7b0ee5a to your computer and use it in GitHub Desktop.
Little Python tools I used

My python scripts for doing stuffs

commons.py contains a prompt function that I used.

rename.py is a tool that renames files based on their MD5 hash.

def get_input(prompt):
"""Displays a yes/no prompt"""
while True:
r = input("%s [y/n]: " % prompt)
r = r.lower().strip()
if r in ("y", "yes"):
return True
elif r in ("n", "no", ""):
return False
from argparse import ArgumentParser
from pathlib import Path
from commons import get_input
def main(args):
input_path = Path(args.input_path)
output_path = Path(args.output_path)
for file in input_path.iterdir():
newname = output_path / file.name[0:1] /file.name
newname.parent.mkdir(exist_ok=True)
if newname.exists():
if get_input("Replace %s?" % newname.relative_to(output_path)):
file.replace(newname)
else:
print("%s is not replaced" % newname.relative_to(output_path))
else:
file.rename(newname)
if __name__ == "__main__":
parser = ArgumentParser(description="Move files based on its initial")
parser.add_argument("input_path")
parser.add_argument("output_path")
args = parser.parse_args()
main(args)
from argparse import ArgumentParser
from pathlib import Path
from hashlib import md5
from commons import get_input
def main(args):
input_path = Path(args.input_path)
output_path = Path(args.output_path)
for file in input_path.iterdir():
m = md5(file.read_bytes())
newname = output_path / (m.hexdigest() + "".join(file.suffixes))
if newname.exists():
if get_input("Replace %s?" % newname.relative_to(output_path)):
file.replace(newname)
else:
print("%s is not replaced" % newname.relative_to(output_path))
else:
file.rename(newname)
if __name__ == "__main__":
parser = ArgumentParser(description="Rename file based on its MD5")
parser.add_argument("input_path")
parser.add_argument("output_path")
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment