DL from the URL to check the byte value and the file will be overwhelmed, so just put it in the trash * ⌒ ヾ (* ´- `) programming.
(Addition) This time, since the URL destination is only the URL saved in the DB, we took the form of saving it locally. It is said that it is not necessary to save it in the end, which will be described later.
#Import library
import urllib.request
import os.path
# URL,Specify the path to save
url = "https://xxxxxxx.co.jp/1234567.mp4"
save_name = "sample.mp4"
#to download
mem = urllib.request.urlopen(url).read()
#Save to file
with open(save_name, mode="wb") as f:
f.write(mem)
#Earn bytes
ret = os.path.getsize(save_name)
print(ret)
#Delete file
os.remove(save_name)
~~ I wanted to get the byte value from the downloaded thing in the memory, but it seemed to be useless unless I dropped it locally, so I wonder if there is another good way. ~~
The file size can be obtained from the data in the memory.
mem = urllib.request.urlopen(url).read()
print(len(mem))
Apart from that, if you use the HEAD method in urllib.request, you can get only the header information and get the file size. (From comments)
I thought I had to study properly because it was my first time without even looking at the introductory book on Python. Reflection (´ ・ ω ・ `)
Recommended Posts