Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'memory.': 0.05; 'completeness': 0.07; 'python': 0.09; 'iterate': 0.09; 'received:155': 0.09; 'subject:not': 0.11; 'sat,': 0.15; "'r').read()": 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'instead:': 0.16; 'oct': 0.16; 'received:155.180': 0.16; 'received:155.180.234': 0.16; 'received:159.53': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'wrote:': 0.17; 'string,': 0.17; 'unicode': 0.17; 'memory': 0.18; 'windows': 0.19; 'to:name:python- list@python.org': 0.20; 'file.': 0.20; 'trying': 0.21; 'task': 0.23; 'random': 0.24; 'received:169.254': 0.24; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'done.': 0.27; 'possibility': 0.27; '2.3': 0.27; 'accuracy': 0.27; 'converting': 0.27; 'instead.': 0.27; 'chris': 0.28; '>>>>': 0.29; 'received:169': 0.29; 'header:Received:8': 0.30; 'file': 0.32; 'could': 0.32; 'doubt': 0.33; 'to:addr:python-list': 0.33; 'text': 0.34; 'done': 0.34; 'follows:': 0.35; 'something': 0.35; 'but': 0.36; 'closing': 0.36; 'charset:us-ascii': 0.36; 'subject:: ': 0.38; 'files': 0.38; 'shows': 0.38; 'there,': 0.38; 'to:addr:python.org': 0.39; 'help': 0.40; 'close': 0.63; 'information,': 0.63; 'url:email': 0.63; 'more': 0.63; 'legal': 0.65; '20,': 0.65; 'subject': 0.66; 'purchase': 0.67; 'physical': 0.69; 'sale': 0.76; 'have?': 0.84; 'received:169.254.8': 0.84; 'subject:available': 0.96 X-DKIM: OpenDKIM Filter v2.1.3 sz1.jpmchase.com q9JJd8KB027329 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1350675548; bh=5xcLH6uqfBCd2HjwtAqWXcm8yMwgaeIJj0yH8+WweX4=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=OW6RzanR5JIHXiHYwTO4A+4xYPGY+CGBXX2lZmMRenBi3JOuMmu0nUFON0gjNBVHp GPomoDOscpCbB0p2ltMT1Ell/N0r/vElyZ3BAzX2QX3NbLSnjZ/HDapHA4LRgGS66u RrfgiGOcQgInT1rRcXf4PIQNA01O9C5wNGL5oULk= From: "Prasad, Ramit" To: "python-list@python.org" Subject: RE: Python does not take up available physical memory Thread-Topic: Python does not take up available physical memory Thread-Index: Ac2uHGpaUb5JSqdJR1m8LKyDAm0X3wAM7IAAAAfSgYA= Date: Fri, 19 Oct 2012 19:38:54 +0000 References: <76D03718A3233B4C8CC236C169B535B5A23E063E45@AUSP01VMBX08.collaborationhost.net> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.67.79.47] Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-DLP-FWD: Yes Content-Type: text/plain; charset="us-ascii" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350675556 news.xs4all.nl 6852 [2001:888:2000:d::a6]:36249 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31763 Chris Angelico wrote:=0D=0A> On Sat, Oct 20, 2012 at 4:08 AM, Pradipto Bane= rjee=0D=0A> wrote:=0D=0A> > I am= trying to read a file into memory=2E The size of the file is around 1 GB= =2E=0D=0A> > I have a 3GB memory PC and the Windows Task Manager shows 2= =2E3 GB available=0D=0A> > physical memory when I was trying to read the fi= le=2E I tried to read the file=0D=0A> > as follows:=0D=0A> >=0D=0A> >=0D=0A= > >=0D=0A> >>>> fdata =3D open(filename, 'r')=2Eread()=0D=0A> =0D=0A> Is th= is Python 2 or Python 3? Just throwing a random possibility out=0D=0A> ther= e, could it be that reading it in and converting it to Unicode=0D=0A> text = requires more memory than you have?=0D=0A> =0D=0A> My recommendation: Unles= s you actually need to search the whole file=0D=0A> as a single string, ite= rate over the file instead:=0D=0A> =0D=0A> for line in open(filename):=0D= =0A> # do something with line=0D=0A=0D=0AIf you (OP) are in Python 2=2E= 5+ I would do the following instead=2E=0D=0A=0D=0Awith open(filename) as f:= =0D=0A for line in f:=0D=0A # do something with line=0D=0A=0D=0AT= his will automatically close the file when it is done=2E I doubt=0D=0Ait wi= ll help with memory issues, but closing files after you=0D=0Aare done with = them is a Good practice=2E=0D=0A=0D=0A=0D=0ARamit Prasad=0D=0A=0D=0A=0D=0AT= his email is confidential and subject to important disclaimers and=0D=0Acon= ditions including on offers for the purchase or sale of=0D=0Asecurities, ac= curacy and completeness of information, viruses,=0D=0Aconfidentiality, lega= l privilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ej= pmorgan=2Ecom/pages/disclosures/email=2E