w3resource logo


Python Exercises

Python File I/O: Get the file size of a plain file

Python File I/O: Exercise-11 with Solution

Write a Python program to get the file size of a plain file.

Contain of text.txt

What is Python language?                                                
Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.                                                 
Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in 
languages such as C++ or Java.                                         
Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles.          
It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.                           
The best way we learn anything is by practice and exercise questions. We  have started this section for those (beginner to intermediate) who are 
familiar with Python.

Output :

File size in bytes of a plain file:  724

Sample Solution :-

Python Code :

def file_size(fname):
        import os
        statinfo = os.stat(fname)
        return statinfo.st_size

print("File size in bytes of a plain file: ",file_size("test.txt"))

Python Code Editor:

def file_size(fname):
        import os
        statinfo = os.stat(fname)
        return statinfo.st_size

print("File size in bytes of a plain file: ",file_size("test.txt"))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.