Skip to content

Instantly share code, notes, and snippets.

@blha303
Last active December 29, 2015 12:29
Show Gist options
  • Save blha303/7670410 to your computer and use it in GitHub Desktop.
Save blha303/7670410 to your computer and use it in GitHub Desktop.
Get total size of files indicated by a list of URLs
from urllib2 import urlopen
import sys
from os import sep
filename = ".".join(sys.argv[0].split(sep)[-1].split(".")[:-1])
def sizeof_fmt(num):
for x in ['bytes','KB','MB','GB','TB','PB','EB','ZB']:
if num < 1024.0 and num > -1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, 'YB')
def main(inp='list.txt', out=filename+'output.txt'):
bytes = 0
with open(inp) as f:
urls = [url.strip() for url in f.readlines()]
with open(out,'w') as output:
for x in urls:
a = urlopen(x)
bytes = bytes + int(a.headers["Content-Length"])
name = x.split("/")[-1]
out = "File: {0:35} | Size: {1:7} | Subtotal: {2}\n".format(name[:32]+"..." if len(name)>35 else name, sizeof_fmt(int(a.headers["Content-Length"])), sizeof_fmt(bytes))
output.write(out)
print out.strip()
print "Total: " + sizeof_fmt(bytes)
return 0
if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) >= 3:
sys.exit(main(inp=sys.argv[1], out=sys.argv[2]))
elif len(sys.argv) >= 2:
sys.exit(main(inp=sys.argv[1]))
else:
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment