Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Is there a built-in function for getting the size of a file object in bytes? I see some people do something like this:

def getSize(fileobject):

fileobject.seek(0,2) # move the cursor to the end of the

file size = fileobject.tell() 

return size 

file = open('myfile.bin', 'rb') 

print getSize(file)

But from my experience with Python, it has a lot of helper functions so I'm guessing maybe there is one built-in.

1 Answer

0 votes
by (106k points)

For getting the file size in Python you may use os.stat() function, which is a wrapper of system call stat():

import os 

def getSize(filename): 

st = os.stat(filename) 

return st.st_size

Related questions

+1 vote
1 answer
asked Jul 9, 2019 in Python by selena (1.6k points)
0 votes
1 answer
asked Jun 26, 2019 in Python by Anurag (33.1k points)
0 votes
1 answer
asked Dec 25, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Jul 15, 2019 in Azure by Eresh Kumar (45.3k points)
0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.6k points)

Browse Categories

...