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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:29:15+00:00 2026-06-04T10:29:15+00:00

I am currently programming a game that requires reading and writing lines in a

  • 0

I am currently programming a game that requires reading and writing lines in a text file. I was wondering if there is a way to read a specific line in the text file (i.e. the first line in the text file). Also, is there a way to write a line in a specific location (i.e. change the first line in the file, write a couple of other lines and then change the first line again)? I know that we can read lines sequentially by calling:

f.readline()

Edit: Based on responses, apparently there is no way to read specific lines if they are different lengths. I am only working on a small part of a large group project and to change the way I’m storing data would mean a lot of work.

But is there a method to change specifically the first line of the file? I know calling:

f.write('text')

Writes something into the file, but it writes the line at the end of the file instead of the beginning. Is there a way for me to specifically rewrite the text at the beginning?

  • 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-06-04T10:29:17+00:00Added an answer on June 4, 2026 at 10:29 am

    If all your lines are guaranteed to be the same length, then you can use f.seek(N) to position the file pointer at the N’th byte (where N is LINESIZE*line_number) and then f.read(LINESIZE). Otherwise, I’m not aware of any way to do it in an ordinary ASCII file (which I think is what you’re asking about).

    Of course, you could store some sort of record information in the header of the file and read that first to let you know where to seek to in your file — but at that point you’re better off using some external library that has already done all that work for you.

    Unless your text file is really big, you can always store each line in a list:

    with open('textfile','r') as f:
        lines=[L[:-1] for L in f.readlines()]  
    

    (note I’ve stripped off the newline so you don’t have to remember to keep it around)

    Then you can manipulate the list by adding entries, removing entries, changing entries, etc.
    At the end of the day, you can write the list back to your text file:

    with open('textfile','w') as f:
        f.write('\n'.join(lines))
    

    Here’s a little test which works for me on OS-X to replace only the first line.

    test.dat

    this line has n characters
    this line also has n characters
    

    test.py

    #First, I get the length of the first line -- if you already know it, skip this block
    f=open('test.dat','r')
    l=f.readline()
    linelen=len(l)-1
    f.close()
    
    #apparently mode='a+' doesn't work on all systems :( so I use 'r+' instead
    f=open('test.dat','r+')
    f.seek(0)
    f.write('a'*linelen+'\n') #'a'*linelen = 'aaaaaaaaa...' 
    f.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im currently reading a book about programming Android and there is a nice little
I'm an independant game developer/designer, and I'm wondering what specific benefits are there to
Hey guys currently the way I frame my sprites in my Android game requires
First off, I'm programming a game. Currently in the render function there are two
With reference to this programming game I am currently building. alt text http://img12.imageshack.us/img12/2089/shapetransformationf.jpg To
I'm currently programming a client application and I'm wondering whether I should use the
I am interested in programming for games. I am currently building an iphone game
I'm writing a game in Java and this is the first one that I'm
I'm currently programming an application that manages parallel rendering on several computers. For that,
I'm programming a console game, (pac-man), and I was wondering how I would get

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.