Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 545005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:42:56+00:00 2026-05-13T10:42:56+00:00

I have a python code which reads many files. but some files are extremely

  • 0

I have a python code which reads many files.
but some files are extremely large due to which i have errors coming in other codes.
i want a way in which i can check for the character count of the files so that i avoid reading those extremely large files.
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:42:57+00:00Added an answer on May 13, 2026 at 10:42 am
    os.stat(filepath).st_size
    

    Assuming by ‘characters’ you mean bytes. ETA:

    i need total character count just like what the command ‘wc filename’ gives me unix

    In which mode? wc on it own will give you a line, word and byte count (same as stat), not Unicode characters.

    There is a switch -m which will use the locale’s current encoding to convert bytes to Unicode and then count code-points: is that really what you want? It doesn’t make any sense to decode into Unicode if all you are looking for is too-long files. If you really must:

    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() gets the locale encoding, reproducing what wc -m does. If you know the encoding yourself (eg. ‘utf-8’) then pass that in instead.

    I don’t think you want to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some python code which runs every 10 minutes or so. It reads
I have created some python code which creates an object in a loop, and
I have some python code which generates a 256^3 numpy array of data that
I have some python code that I wrote to convert a python list into
I have some python code using shutil.copyfile: import os import shutil src='C:\Documents and Settings\user\Desktop\FilesPy'
I have some python code that currently performs expensive computation by performing the computation
I have some really nice Python code to do what I need to do.
I have to port an algorithm from an Excel sheet to python code but
I'm trying to write some python code which can create multipart mime http requests
I am converting some code from another language to python. That code reads a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.