Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get file size of the file from the file object

I need to know the size of the file based on file object

import csv
import os
with open("test.csv", "rb") as infile:
    reader = csv.reader(infile)
    print reader
    filesize(reader)
def filesize(reader):
    os.getsize(reader) #And i need work with reader for more details.so I must need to pass a reader or file object

When I run this I got an output is

<_csv.reader object at 0x7f5644584980>

From this file object how I get the size of the file?

And I checked this site but these are not CSV attribute size of an open file object

EDIT: When I use that two inbuilt function I got errors that are

AttributeError: '_csv.reader' object has no attribute 'seek' AttributeError: '_csv.reader' object has no attribute 'tell'

like image 970
bob marti Avatar asked Oct 21 '25 03:10

bob marti


2 Answers

Adding this answer because it actually answers the question asked about using the file object directly:

import csv
import os
with open("test.csv", "rb") as infile:
    reader = csv.reader(infile)
    print reader
    infile.seek(0, os.SEEK_END)
    filesize = infile.tell()
like image 116
princelySid Avatar answered Oct 23 '25 20:10

princelySid


You can use os.path.getsize or os.stat

import os
os.path.getsize('test.csv')

OR

os.stat('test.csv').st_size 

Return the size, in bytes.

like image 20
Dineshkarthik Raveendran Avatar answered Oct 23 '25 19:10

Dineshkarthik Raveendran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!