Python Forum
check if a file exist on the internet and get the size
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check if a file exist on the internet and get the size
#1
hi, sorry for my bad english,
i currently create this code:
#image_name = 'https://www.google.co.id/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'
image_name = 'C:\DumpStack.log'
from os.path import exists
if exists(image_name):
    import os
    statinfo = os.stat(image_name)
    print(image_name + " exist, with size : " + str(statinfo.st_size))
else:
    print(image_name + " do not exist")
but somehow does not work at a file on the internet,
can someone convert this code or give me the clue on the google keyword?
thank you, have a nice day
Reply
#2
(Apr-16-2022, 12:36 PM)kucingkembar Wrote: but somehow does not work at a file on the internet,
For internet need something like Requests .
The HEAD request has info about content-length.
The headers dos not always have true size,so can use stream=True to get only response header when download(not whole response body)
import requests

url = 'https://www.google.co.id/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'
response = requests.get(url, stream=True).headers['Content-length']
print(response)
Output:
5969
To download image it would be like this.
import requests 

img = requests.get(url)
with open('logo.png', 'wb') as f:
    f.write(img.content)
Some tips about your code,import should always first in code.
Look into f-string.
>>> image_name = 'logo.png'
>>> stat = 200
>>> # print(image_name + " exist, with size : " + str(statinfo.st_size))
>>> # Become
>>> print(f'{image_name} exist, with size: {stat}kb')
logo.png exist, with size: 200kb
kucingkembar likes this post
Reply
#3
thank you snippsat for the reply,
now I know how to get internet file size,
but I still don't understand how to get: if the URL is a file,
if I change a letter in the URL and get an Error 404 (Not Found),
the code still responds: the file exists with size xxxx

and about "import should always first in code"
-is there any problem to put "import" later in the code?
-what happens if put "import" the same package twice?

thank you again for your reply
Reply
#4
An HTTP response need not be the contents of a file and the Content-Length header simply contains the size of that response. So, in the case of a 404 where the server sent something in the response body, you'll still get a non-zero value for Content-Length.

import statements should be listed first mainly for readability purposes - they interrupt your flow of reading and understanding the code if they're in the middle.
Reply
#5
@ndc85430 thank you for the reply,
my problem is I need to download the jpg if the jpg URL is a real jpg,
if the URL is not jpg, I will not need to download it,

I always put the import when it is necessary to load, especially inside the function/def,
they said python is slow to run, and CMIIW, I guess: only "load/import" when the necessary packages really needed; is a way to make it faster
Reply
#6
You might want to try checking the value of the Content-Type header then.
Reply
#7
@ndc85430 Dude, thank you, i sent you and snippsat reputation +1 point
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 444 Mar-11-2024, 11:02 AM
Last Post: snippsat
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,621 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Converted EXE file size is too large Rajasekaran 0 1,448 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  please check this i wanna use a csv file as a graph xCj11 5 1,439 Aug-25-2022, 08:19 PM
Last Post: deanhystad
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,877 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  SQLALCHEMY - Column doesn't exist jamesaarr 9 7,437 Nov-04-2021, 09:20 AM
Last Post: ndc85430
  Check last time file was accessed Pavel_47 4 2,761 Jun-01-2021, 05:47 PM
Last Post: Yoriz
  How to check if a file has finished being written leocsmith 2 7,694 Apr-14-2021, 04:21 PM
Last Post: perfringo
  pathlib destpath.exists() true even file does not exist NaN 9 4,566 Dec-01-2020, 12:43 PM
Last Post: NaN
  Check internet speed ebolisa 1 1,736 Oct-29-2020, 08:09 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020