Skip to content

Instantly share code, notes, and snippets.

@masatana
Last active August 29, 2015 14:10
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 masatana/c532c6fc90098fb6ea99 to your computer and use it in GitHub Desktop.
Save masatana/c532c6fc90098fb6ea99 to your computer and use it in GitHub Desktop.
指定ディレクトリ(デフォルト: カレントディレクトリ)の指定サイズ以下(デフォルト: 4096 bytes)のファイルを削除するスクリプト
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import argparse
from glob import glob
from os.path import isdir, getsize, abspath, exists, join
def main():
parser = argparse.ArgumentParser(description="Cleanup some files.")
parser.add_argument("--path", type=str, default=".",
help="target path to cleanup")
parser.add_argument("--size", type=int, default=4096,
help="the maximum size to cleanup (byte)")
args = parser.parse_args()
normalized_path = abspath(args.path)
if not exists(normalized_path):
print("{} doesn't exist!".format(args.path))
return 1
for filename in glob(normalized_path + "/*"):
if not isdir(filename) and getsize(filename) < args.size:
os.remove(filename)
return
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment