Last active
August 29, 2015 14:10
-
-
Save masatana/c532c6fc90098fb6ea99 to your computer and use it in GitHub Desktop.
指定ディレクトリ(デフォルト: カレントディレクトリ)の指定サイズ以下(デフォルト: 4096 bytes)のファイルを削除するスクリプト
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 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