4

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'

Community
  • 1
  • 1
bob marti
  • 1,523
  • 3
  • 11
  • 27
  • @I told that into my question i tested in this site.It is not valid in csv file – bob marti Apr 07 '17 at 12:34
  • 1
    Possible duplicate of [Size of an open file object](http://stackoverflow.com/questions/283707/size-of-an-open-file-object) – CristiFati Apr 07 '17 at 12:34
  • 2
    Can't you use the `infile` variable ? – TrakJohnson Apr 07 '17 at 12:35
  • @CristiFati It is not duplicates.Why you said that it is duplicate.Can u prove that – bob marti Apr 07 '17 at 12:37
  • @bobmarti Its a duplicate because the best method in your case seems to be the one indicated in the accepted answer. – TrakJohnson Apr 07 '17 at 12:38
  • Use `infile` rather than `reader`, when you are getting the size. – CristiFati Apr 07 '17 at 12:39
  • @CristiFati I need to pass the reader into another function?that's why i need get the size based on file object – bob marti Apr 07 '17 at 12:40
  • You can't do that if all you have is a `csv.reader` instance. It doesn't provide a way to access the input file. – Aran-Fey Apr 07 '17 at 12:49
  • After the last edit, the question has the appropriate context (which qualifies it as a non dup). But it's impossible. `csv.reader` receives an iterable as arg (it could also be a list). Why do you need to get the size from reader? If size is important, you could get it from `infile` and "forward" it tho whatever routine needs it. – CristiFati Apr 07 '17 at 13:11
  • if i pass the infile what is the differcence beteen passing the file and infile.To avoid that i need to pass reader.And size is important.And the main thing is to avoid that open the file multiple times – bob marti Apr 07 '17 at 13:16

3 Answers3

4

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.

Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51
4

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()
princelySid
  • 6,687
  • 2
  • 16
  • 17
2

What's wrong with os.path.getsize?

With your code:

import os
import csv
with open("test.csv", "rb") as infile:
   reader = csv.reader(infile)
   print os.path.getsize(infile.name)

The size is in bytes.

Or Duan
  • 13,142
  • 6
  • 60
  • 65
  • @duan I need to pass the reader into another function then how i get the solution – bob marti Apr 07 '17 at 12:41
  • @bobmarti, you can't access the file/path from the reader, it design to get an iterable, not a file, you will have to pass the filesize/file object to your other function. – Or Duan Apr 07 '17 at 13:18