简体   繁体   中英

how can I limit downloaded file size in python?

I am trying to limit downloaded file size to only < 1MB files, but my code is somehow buggy because it downloads > 1MB, however, when I tried to test it downloads more than 1mb file

self.url = 'https://www.google.com/search?q=filetype:{}+{}&num={}'.format(self.ext, self.magic_header, self.max)
self.MAX_SIZE = 1000000 # 1024 * 1024 this doesnt work either
try:
            response = requests.head(href)
            total = response.headers.get('content-length')
            if int(total) > self.MAX_SIZE:
              print "maximum size (%d kbs)" % (self.MAX_SIZE/1024)
            else:
              if total is None:
                pass
              else:
                #if os.path.exists(OUTPUT_DIR):
                #  print("Deleting old output directory")
                #  shutil.rmtree(OUTPUT_DIR)
                #print("Creating output directory")
                #os.mkdir(OUTPUT_DIR)
                os.system('wget -P %s %s'%(OUTPUT_DIR, href))
          except Exception as e:
            pass```

output

204K 'NetLogo Tutorial 1 in Spanish.pdf'  1.2M  zElquehacertutorial.pdf
916K  proceso_tutorial_de_la_mcdst.pdf    2.3M  z-El-sistema-tutorial-en-la-UV.pdf

if you have the file object, you can use this

import os
os.fstat(f.fileno()).st_size > self.MAX_SIZE:

os.fstat(f.fileno()).st_size will give the file's size in byte

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM