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

  • SEARCH
  • Home
  • 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 3480642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:22:22+00:00 2026-05-18T10:22:22+00:00

Is there a limit to memory for python? I’ve been using a python script

  • 0

Is there a limit to memory for python? I’ve been using a python script to calculate the average values from a file which is a minimum of 150mb big.

Depending on the size of the file I sometimes encounter a MemoryError.

Can more memory be assigned to the python so I don’t encounter the error?


EDIT: Code now below

NOTE: The file sizes can vary greatly (up to 20GB) the minimum size of the a file is 150mb

file_A1_B1 = open("A1_B1_100000.txt", "r")
file_A2_B2 = open("A2_B2_100000.txt", "r")
file_A1_B2 = open("A1_B2_100000.txt", "r")
file_A2_B1 = open("A2_B1_100000.txt", "r")
file_write = open ("average_generations.txt", "w")
mutation_average = open("mutation_average", "w")

files = [file_A2_B2,file_A2_B2,file_A1_B2,file_A2_B1]

for u in files:
    line = u.readlines()
    list_of_lines = []
    for i in line:
        values = i.split('\t')
        list_of_lines.append(values)

    count = 0
    for j in list_of_lines:
        count +=1

    for k in range(0,count):
        list_of_lines[k].remove('\n')

    length = len(list_of_lines[0])
    print_counter = 4

    for o in range(0,length):
        total = 0
        for p in range(0,count):
            number = float(list_of_lines[p][o])
            total = total + number
        average = total/count
        print average
        if print_counter == 4:
            file_write.write(str(average)+'\n')
            print_counter = 0
        print_counter +=1
file_write.write('\n')
  • 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-18T10:22:23+00:00Added an answer on May 18, 2026 at 10:22 am

    (This is my third answer because I misunderstood what your code was doing in my original, and then made a small but crucial mistake in my second—hopefully three’s a charm.

    Edits: Since this seems to be a popular answer, I’ve made a few modifications to improve its implementation over the years—most not too major. This is so if folks use it as template, it will provide an even better basis.

    As others have pointed out, your MemoryError problem is most likely because you’re attempting to read the entire contents of huge files into memory and then, on top of that, effectively doubling the amount of memory needed by creating a list of lists of the string values from each line.

    Python’s memory limits are determined by how much physical ram and virtual memory disk space your computer and operating system have available. Even if you don’t use it all up and your program “works”, using it may be impractical because it takes too long.

    Anyway, the most obvious way to avoid that is to process each file a single line at a time, which means you have to do the processing incrementally.

    To accomplish this, a list of running totals for each of the fields is kept. When that is finished, the average value of each field can be calculated by dividing the corresponding total value by the count of total lines read. Once that is done, these averages can be printed out and some written to one of the output files. I’ve also made a conscious effort to use very descriptive variable names to try to make it understandable.

    try:
        from itertools import izip_longest
    except ImportError:    # Python 3
        from itertools import zip_longest as izip_longest
    
    GROUP_SIZE = 4
    input_file_names = ["A1_B1_100000.txt", "A2_B2_100000.txt", "A1_B2_100000.txt",
                        "A2_B1_100000.txt"]
    file_write = open("average_generations.txt", 'w')
    mutation_average = open("mutation_average", 'w')  # left in, but nothing written
    
    for file_name in input_file_names:
        with open(file_name, 'r') as input_file:
            print('processing file: {}'.format(file_name))
    
            totals = []
            for count, fields in enumerate((line.split('\t') for line in input_file), 1):
                totals = [sum(values) for values in
                            izip_longest(totals, map(float, fields), fillvalue=0)]
            averages = [total/count for total in totals]
    
            for print_counter, average in enumerate(averages):
                print('  {:9.4f}'.format(average))
                if print_counter % GROUP_SIZE == 0:
                    file_write.write(str(average)+'\n')
    
    file_write.write('\n')
    file_write.close()
    mutation_average.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any limit on the physical memory size (file system) one can utilize
In a Python script, I want to set a memory limit for a certain
Is there any way to use Zend_Filter_Encrypt with large files, without rising memory limit
Is there a limit as far as NSString is concerned ?? I am using
Is there any limit on how much data can be stored using GM_setValue ?
How many interfaces can a class file implement? Is there a limit on the
I'm trying to find a way to limit the memory available for the Python
I sometimes write Python programs which are very difficult to determine how much memory
Is there a limit on how much data I can store using jQuery.data( element,
is there any memory limit for a single process in x64 Linux? we are

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.