4 Methods to Get Python File Size Easily

In this article, we will cover information about our talking point in python – get file size. Knowing the size of any file in python programming is crucial because it determines if you can use the following file in your program. Certain programs can support only a particular file type and size; therefore, file handling is vital, and knowing file size is a significant part of file handling.

There exist many ways in python to get file size. We will discuss four methods in this article, three of the methods use the os module in python, and one uses the ‘pathlib’ module. Os module in python enables interaction with the operating system of the computer. Besides, the os module ‘pathlib’ library eases the process the deal with file paths. To know more about the os module, check out python’s official documentation.

Python Get File Size Method 1

Methods to Get Python File Size Easily

Firstly we will discuss three methods that use the os module but are different in usage and also have different syntaxes. Method one is ‘os.stat()’ function. This function needs a file path as an argument and, in return, provides statistical data about the file. It can also be used to get file size.

To use ‘os.stat(),’ you need to follow the steps given below:

  1. Import the os module into your code.
  2. Then use the syntax os.stat( ‘insert your file path here’ ).
  3. Print the results.

Let’s understand better with the help of an example

Example 1

import os

size = os.stat(r'C:\mydata\testdata1.txt')
print("File size:", size,"bytes")

Output:

File size: 204318 bytes

As you can see, this function determines file size in terms of bytes.

Python Get File Size Method 2

In method 2, we will use ‘os.path.getsize()’ function, which similarly takes the file path as an argument and, in return, provides file size in bytes.

Follow the steps given below to use this function:

  1. Import os module in your code.
  2. Use the syntax os.path.getsize( ‘insert your file path here’ ).
  3. Print the results.

Let’s understand better with the help of an example.

Example 2

import os
 
size = os.path.getsize(r'C:\mydata\testdata2.txt')
print("File size:", size, "bytes")

Output:

File size: 102284 bytes

Python Get File Size Method 3

In addition to methods 1 & 2, method 3 also uses the os module. Although we use different functions to get file size, ‘open()’ function to open a file and ‘seek()’ function to get file size.

Follow the steps given below to get file size:

  1. Import os module.
  2. Open the file using ‘open()’ function.
  3. Get file size using ‘seek()’ function.
  4. Print the results.

Example 3

import os
with  open(r'C:\mydata\testdata2.txt') as size:
    size.seek(0, os.SEEK_END)
    print('File size:', size.tell(), 'bytes')

Output:

File size: 102284 bytes

Python Get File Size Method 4

In method 4, we will use the ‘pathlib’ module. ‘pathlib’ module offers classes representing filesystem paths along with semantics which is appropriate for different computer operating systems. Moreover ‘patlib’ module offers inbuilt functions which are capable of providing file size.

Further, to get the file size, follow the steps given below:

  1. Import ‘pathlib’ in your code in addition to that from path ‘parthlib’ module import Path. Although this can be done in one line of code.
  2. Use .stat() to open the file
  3. Further, use .st_size as shown below in the example code to get the file size.
  4. Print the results.

Example 4

from pathlib import Path

Path(r'C:\mydata\testdata1.txt').stat()
size =Path(r'C:\mydata\testdata4.txt').stat().st_size
print("File size:", size, "bytes")

Output:

File size: 331560 bytes

Python Get File Size In KB, MB & GB.

All the methods we covered above provide us data in bytes nevertheless, you might want file size in KB, MB, or GB. Thus to do so, you can use the example given below to convert your files to your desired size.

Example 5

import os.path

path = r'C:\mydata\testdata2.txt'
size = os.path.getsize(path)

print('File size in Bytes: ' + str(size))
print('File size in KB: ' + str(size/1024))
print('File size in MB: ' + str(size/1024**2))
print('File size in GB: ' + str(size/1024**3))

Output:

File size in Bytes: 331560
File size in KB: 323.78906
File size in MB: 0.3162
File size in GB: 0.00031

In the above example we have used ‘os.path.getsize()’ function. Although you can use your preferred function to get the file size and then use the syntax given in the example to convert it to various sizes.

Python Get File Creation and Modification Date

Sometimes you might want to see when the file was created or when it was last modified. To do that, you need to follow the steps given below:

  1. Import os.path module.
  2. From os.path import datetime.
  3. Use syntax time.ctime(os.path.getmtime(r’your file path’)) to get modification date.
  4. Use syntax time.ctime(os.path.getctime(r’your file path’)) to get last creation date.

Example 6

from os.path import datetime
print(time.ctime(os.path.getmtime(r'C:\mydata\testdata1.txt')))
print(time.ctime(os.path.getctime(r'your file path')))

Output:

Fri Jul 29 19:45:23 2022
Fri Jul 29 19:45:23 2022

Get File Size From Url

If you want to get file size from url of a file, you can use the urllib package and follow the given steps:

  1. Import urllib package.
  2. Define a variable containing the url.
  3. use ‘length()’ function to get file size.

Example 7

import urllib.request
file=urllib.request.urlopen("https://sample-videos.com/xls/Sample-Spreadsheet-10-rows.xls")
print("File size:", file.length, "bytes")

Output:

File size: 16102 bytes

FAQs on Python get file size

How do you get the file size of all files in a directory?

You can use the ‘scandir()’ function to get the file size of the directory.

How to get the file size of the CSV file?

All the methods mentioned in this article can be used to get the size of the CSV file.

How to get python file content length?

You can get python file content length using the methods discussed in this article.

Conclusion

In this article, we saw how different functions can be used to get the file size and how they are different in terms of technicality and usage. One may use these functions however their code allows them to. To know more about files and understand their work with python, check this post.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments