Get File Size With Python

In this post, we will learn how to get a file size with Python using a build-in function.

Using Python build-in function, we can measure a file size using Python len function.

Code

In the below code example, I’m opening and reading a file and using the len function to measure the size of the file and print it on the screen.

myfile = open ("file1.txt")
size = myfile.read()
print (f"File size is: {len(size)}")

The above code is useful if you would like to print the result or use the size as part of an if condition.

Leave a comment

Your email address will not be published. Required fields are marked *