[英]how can I limit downloaded file size in python?
我試圖將下載的文件大小限制為僅 < 1MB 的文件,但我的代碼存在某種錯誤,因為它下載 > 1MB,但是,當我嘗試測試它時下載超過 1mb 的文件
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
如果你有 object 文件,你可以使用這個
import os
os.fstat(f.fileno()).st_size > self.MAX_SIZE:
os.fstat(f.fileno()).st_size 將以字節為單位給出文件的大小
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.