When working with files in Python, it’s often necessary to check the size of a file. The size of a file can be important for a variety of reasons, such as ensuring that the file is not too large to be transferred over the network or determining how much storage space the file will take up. In this article, we will discuss how to write a Python program to check the file size.

Python provides a built-in module called os that can be used to check various properties of a file, including its size. Here’s a simple example:

import os

# Get the file path from the user

file_path = input(“Enter the file path: “)

# Check if the file exists

if os.path.isfile(file_path):

    # Get the size of the file in bytes

    file_size = os.path.getsize(file_path)

    # Display the result

    print(“The size of the file is {} bytes”.format(file_size))

else:

    print(“The file does not exist”)

In this program, we first import the os module. We then prompt the user to enter the path of the file they want to check using the input() function.

Next, we use the os.path.isfile() function to check if the file exists. If the file exists, we use the os.path.getsize() function to get the size of the file in bytes. Finally, we display the result using string formatting.

Let’s run this program with a sample file:

Output

Enter the file path: C:UsersTecAdminDocumentstest.csv The size of the file is 108733 bytes

As you can see, the program correctly displays the size of the file in bytes.

It’s worth noting that the size of a file can also be expressed in other units, such as kilobytes, megabytes, or gigabytes. To convert the size of a file to a different unit, you can use simple arithmetic operations. For example, to convert the size of a file from bytes to kilobytes, you can divide the size by 1024. Here’s an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

import os

# Get the file path from the user

file_path = input(“Enter the file path: “)

# Check if the file exists

if os.path.isfile(file_path):

    # Get the size of the file in bytes

    file_size = os.path.getsize(file_path)

    # Convert the size to kilobytes

    file_size_kb = file_size / 1024

    # Display the result

    print(“The size of the file is {:.2f} kilobytes”.format(file_size_kb))

else:

    print(“The file does not exist”)

In this program, we divide the size of the file by 1024 to convert it from bytes to kilobytes. We then use string formatting to display the result with two decimal places.

Let’s run this program with the same file as before:

Output

Enter the file path: C:UsersTecAdminDocumentsbackup.zip The size of the file is 2229.92 kilobytes

As you can see, the program correctly displays the size of the file in kilobytes.

In conclusion, checking the size of a file in Python is a simple task that can be accomplished using the os.path.getsize() function. By understanding the basics of file operations in Python, you can begin to build more complex programs that manipulate files in various ways.