Created
January 24, 2012 23:11
-
-
Save lukeorland/1673391 to your computer and use it in GitHub Desktop.
Prints the combined MB of the files passed as arguments
This file contains hidden or 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 python | |
from __future__ import print_function | |
import argparse | |
import os | |
def file_size(file_name): | |
"""Returns size of the file named file_name in bytes.""" | |
if not os.path.exists(file_name): | |
msg = "The file {} does not exist.".format(file_name) | |
raise argparse.ArgumentTypeError(msg) | |
return os.path.getsize(file_name) | |
def bytes_to_MB(num_bytes): | |
"""Returns float value""" | |
return num_bytes / 1e6 | |
def main(): | |
# Parse the command line arguments. | |
parser = argparse.ArgumentParser(description='Prints the combined MB of ' + | |
'the files passed as arguments.') | |
parser.add_argument('file_names', nargs='*', | |
help='files that are candidates for fitting') | |
parser.add_argument('--bytes', '-b', action='count', | |
help='prints the total number of bytes instead of MB') | |
args = parser.parse_args() | |
file_names = args.file_names | |
bytes = args.bytes | |
total_size = sum([file_size(fn) for fn in file_names]) | |
if bytes: | |
print("{}".format(total_size)) | |
else: | |
print("{}".format(bytes_to_MB(total_size))) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment