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 6062433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:03:01+00:00 2026-05-23T09:03:01+00:00

New to python and trying to learn the ropes of file i/o. Working with

  • 0

New to python and trying to learn the ropes of file i/o.

Working with pulling lines from a large (2 million line) file in this format:

56fr4
4543d
4343d
hirh3

I’ve been reading that readline() is best because it doesn’t pull the whole file into memory. But when I try to read the documentation on it, it seems to be Unix only? And I’m on a Mac.

Can I use readline on the Mac without loading the whole file into memory? What would the syntax be to simply readline number 3 in the file? The examples in the docs are a bit over my head.

Edit

Here is the function to return a code:

def getCode(i):
    with open("test.txt") as file:
        for index, line in enumerate(f):
            if index == i:
                code = # what does it equal?
                break
    return code
  • 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-23T09:03:02+00:00Added an answer on May 23, 2026 at 9:03 am

    You don’t need readline:

    with open("data.txt") as file:
        for line in file:
            # do stuff with line
    

    This will read the entire file line-by-line, but not all at once (so you don’t need all the memory). If you want to abort reading the file, because you found the line you want, use break to terminate the loop. If you know the index of the line you want, use this:

    with open("data.txt") as file:
        for index, line in enumerate(file):
            if index == 2: # looking for third line (0-based indexes)
                # do stuff with this line
                break # no need to go on
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty new to Python world and trying to learn it. This is
I am pretty new to python and working with firmata I am trying to
I'm brand new to Python and trying to learn it by replicating the following
I am relatively new to Python and I am trying to learn a thing
I am new to python and trying to learn. I am trying to implement
I am new to Python and trying to learn the language structure. I understand
New to python and trying to understand multi-threading. Here's an example from python documentation
I'm pretty new to python and am trying to grab the ropes and decided
I am trying to learn Python lists. In this code I am trying to
I am new to Python, trying to learn it by doing a tutorial based

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.