Python urllib how to get the size of file before download














































Python urllib how to get the size of file before download



Description :
urllib is one of the module in python which helps to access the websites using programming. Through this module, one can access the websites, parse it and  download any data, modify the headers and also do any POST and GET requests.
Some websites restricts accessing their data through the programs and they might also block you if they found that someone is trying to accessing their data using program.
We will firstly import urllib.requests. After importing we'll assign a variable the url of website using urlopen() and eventually read using variable name (a) a.read() and that we will get the ASCII text file of the website.

Firstly, you need to intall urllib module of python
Installing urllib : pip install urllib

In this article, I will show how to get size of file using the URL of website.

CODE :

import urllib.request

class Size:
# Constructor takes the choice and url
def __init__(self, choice, url):
self.choice = choice
self.url = url

def findSize(self):
# If entered choice is 1, 2 or 3
if (self.choice >= 1 and self.choice <= 3):
# Size in bytes
if (self.choice == 1):
try:
site = urllib.request.urlopen(self.url)
sizeBytes = len(site.read())
print("File size in bytes : ", sizeBytes)
except Exception as e:
print(str(e))

# Conversion to KB
elif (self.choice == 2):
try:
site = urllib.request.urlopen(self.url)
sizeBytes = len(site.read())
sizeMB = sizeBytes / (1024 * 1024)
print("Size in MB =", "{:.2f}".format(sizeMB))
except Exception as e:
print(str(e))

# Conversion to KB
elif (self.choice == 3):
try:
site = urllib.request.urlopen(self.url)
sizeBytes = len(site.read())
sizeKB = sizeBytes / 1024
print("Size in KB =", "{:.2f}".format(sizeKB))
except Exception as e:
print(str(e))

else:
print("Invalid Entry !!")

def main():
# following choices can be entered
print("1 for size in bytes")
print("2 for size in MB")
print("3 for size in KB")

# Enter a valid URL
print("Enter the URL : ")
url = input()
# Enter a valid choice
choice = int(input("Enter your choice : "))

# Object created for the class FileSize
obj = Size(choice, url)
obj.findSize()

#It calls the main function
if __name__ == "__main__":
main()

Explaination :
We will firstly import urllib.requests module and then fetch the url using urllib.request module.
If all the inputs are entered properly then we will get the size of the file.
We take user input for in which form size is to be displayed i.e., bytes, MB or KB and call the function
findSize for displaying the size of the file.
We have used try and except because if in case an error occurs then we can get to know which type of exception has occured. 

By default size will be displayed in bytes, so in this program we can convert bytes into MB and KB as user wants to see the size of the file.

OUTPUT :

Test case 1:
1 for size in bytes
2 for size in MB
3 for size in KB
Enter the URL : 
https://cppsecrets.com/
Enter your choice : 1
File size in bytes :  65895
-------------------------------
Test case 2:
1 for size in bytes
2 for size in MB
3 for size in KB
Enter the URL : 
https://cppsecrets.com/
Enter your choice : 2
Size in MB = 0.06
-------------------------------
Test case 3:
1 for size in bytes
2 for size in MB
3 for size in KB
Enter the URL : 
https://cppsecrets.com/
Enter your choice : 3
Size in KB = 64.35


Comments