Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# coding: utf-8
$ python __script__ [path] --size [size]
$ # カレントディレクトリ以下の 100 MB 以上のファイルを探す場合
$ python __script__ . --size 100
import argparse
from pathlib import Path
def search_files(path, size_min):
size_min_in_mb = size_min << 20
p = Path(path)
for file in p.iterdir():
if file.is_dir():
search_files(file, size_min)
elif file.is_file():
size = file.stat().st_size
if size >= size_min_in_mb:
print('{:.1f}MB\t{}'.format(size >> 20, file.resolve()))
def arg_validate_args():
"""対象のパスとサイズ( MB )をコマンドライン引数から取得する
parser = argparse.ArgumentParser('Search large files.')
parser.add_argument('path', nargs='?', default='.', help='Path to search.')
parser.add_argument('-s', '--size', dest='size', type=int, required=True, help='Minimum file size (MB)')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = arg_validate_args()
path = args.path
size = args.size
print('Searchinig files larger than or equals to {}MB in "{}"...'.format(size, path))
search_files(path, size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment