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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:12:11+00:00 2026-05-11T22:12:11+00:00

I am writing a file to disk in stages. As I write it I

  • 0

I am writing a file to disk in stages. As I write it I need to know the line numbers that I am writing to use to build an index. The file now has 12 million lines so I need to build the index on the fly. I am doing this in four steps, with four groupings of the value that I am indexing on. Based on some examples I found elsewhere on SO I decided that to keep my functions as clean as possible I would get the linesize of the file before I start writing so I can use that count to continue to build my index.

So I have run across this problem, theoretically I don’t know if I am adding the first chunk or the last chunk to my file so I thought to get the current size I would

myFile=open(r'C:\NEWMASTERLIST\FULLLIST.txt','a')
try:
    num_lines=sum(1 for line in myFile)
except IOError:
    num_lines=0

When I do this the result is always 0-even if myFile exists and has a num_lines >0

If I do this instead:

myFile=open(r'C:\NEWMASTERLIST\FULLLIST.txt')
try:
    num_lines=sum(1 for line in myFile)
except IOError:
    num_lines=0

I get the correct value iff myFile exists. byt if myFile does not exist, if I am on the first cycle, I get an error message.

As I was writing out this question it occurred to me that the reason for the value num_lines=0 on every case the file exists is because the file is being opened for appending to so the file is opened at the last line and is now awaiting for lines to be delivered. So this fixes the problem

try:
    myFile=open(r'C:\NEWMASTERLIST\FULLLIST.txt')
    num_lines=sum(1 for line in myFile)

except IOError:
    num_lines=0

My question is whether or not this can be done another way. The reason I ask is because I have to now close myFile and reopen it for appending:

That is to do the work I need to do now that I have the ending index number for the data that is already in the file I have to

myFile.close()
myFile=open(r'C:\NEWMASTERLIST\FULLLIST.txt','a')

Now, here is where maybe I am learning something- given that I have to open the file twice then maybe getting the starting index (num_lines) should be moved to a function

def getNumbLines(myFileRef):
    try:
        myFile=open(myFileRef)
        num_lines=sum(1 for line in myFile)
        myFile.close()
    except IOError:
        num_lines=0
    return num_lines

It would be cleaner if I did not have to open/handle the file twice.

Based on Eric Wendelin’s answer I can just do:

myFile=open(r'C:\NEWMASTERLIST\FULLLIST.txt','a+')
num_lines=sum(1 for line in myFile)

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-11T22:12:11+00:00Added an answer on May 11, 2026 at 10:12 pm

    You can open a file for reading AND writing:

    myFile=open(r'C:\NEWMASTERLIST\FULLLIST.txt','r+')
    

    Try that.

    UPDATE: Ah, my mistake since the file might not exist. Use ‘a+’ instead of ‘r+’.

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

Sidebar

Related Questions

I have a client node writing a file to a hard disk that is
While writing a file using ofstream, how do I know when the file's size
I'm writing a file manager and need to scan directories and deal with renaming
How would you go about writing a file from emacs that contains only the
I'm writing an Android app where I'm writing a file to disk with one
In java I need to read a binary file from a site and write
I'm writing a couple of classes that generate xml file. (Details probably not important
Does anyone know of a way to zip a stream that you are writing
I'm writing an application that on some stage performs low-level disk operations in Linux
I'm writing a bootsector for a FAT12 file system on a 1.44MB floppy disk

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.