How to check the number of characters in a file in python

I have python code that reads a lot of files. but some files are extremely large, which is why I have errors coming into other codes. I want so that I can check the number of characters in the files so that I do not read these extremely large files. Thanks.

+5
source share
5 answers
os.stat(filepath).st_size

Assuming by characters, you mean bytes. ETA:

I need a common character counter just like the wc filename command gives me unix

In what mode? wcat its discretion will give you a string, the number of words and bytes (the same as stat), and not Unicode characters.

-m, locale Unicode : , ? Unicode, , , . :

import sys, codecs

def getUnicodeFileLength(filepath, charset= None):
    if charset is None:
        charset= sys.getfilesystemencoding()
    readerclass= codecs.getReader(charset)
    reader= readerclass(open(filepath, 'rb'), 'replace')
    nchar= 0
    while True:
        chars= reader.read(1024*32)  # arbitrary chunk size
        if chars=='':
            break
        nchar+= len(chars)
    reader.close()
    return nchar

sys.getfilesystemencoding() , wc -m. (, "utf-8" ), .

, .

+8

, , , .

, , , os.path.getsize(), stat , stat() ( Unix Windows).

+7

Try

import os
os.path.getsize(filePath)

to get your file size in bytes.

+5
source
os.path.getsize(path) 

Returns the size, in bytes, of the path. Raise os.error if the file does not exist or is inaccessible.

+4
source

alternative way

f=open("file")
os.fstat( f.fileno() ).st_size
f.close()
+2
source

All Articles