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

The Archive Base Latest Questions

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

I have file about 4MB (which i called as big one)…this file has about

  • 0

I have file about 4MB (which i called as big one)…this file has about 160000 lines..in a specific format…and i need to cut them at regular interval(not at equal intervals) i.e at the end of a certain format and write the part into another file..

Basically,what i wanted is to copy the information for the big file into the many smaller files …as i read the big file keep writing the information into one file and after the a certain pattern occurs then end this and starting writing for that line into another file…

Normally, if it is a small file i guess it can be done dont know if i can perform file.readline() to read each line check if pattern end if not then write it to a file if patter end then change the file name open new file..so on but how to do it for this big file..

thanks in advance..

didnt mention the file format as i felt it is not neccesary will mention if required..

  • 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-15T10:22:31+00:00Added an answer on May 15, 2026 at 10:22 am

    I would first read all of the allegedly-big file in memory as a list of lines:

    with open('socalledbig.txt', 'rt') as f:
        lines = f.readlines()
    

    should take little more than 4MB — tiny even by the standard of today’s phones, much less ordinary computers.

    Then, perform whatever processing you need to determine the beginning and ending of each group of lines you want to write out to a smaller files (I’m not sure by your question’s text whether such groups can overlap or leave gaps, so I’m offering the most general solution where they’re fully allowed to — this will also cover more constrained use cases, with no real performance penalty, though code might be a tad simpler if the constraints were very rigid).

    Say that you put these numbers in lists starts (index from 0 of first line to write, included), ends (index from 0 of first line to NOT write — may legitimately and innocuosly be len(lines) or more), names (filenames to which you want to write), all lists having the same length of course.

    Then, lastly:

    assert len(starts) == len(ends) == len(names)
    
    for s, e, n in zip(starts, ends, names):
        with open(n, 'wt') as f:
            f.writelines(lines[s:e])
    

    …and that’s all you need to do!

    Edit: the OP seems to be confused by the concept of having these lists, so let me try to give an example: each block written out to a file starts at a line containing 'begin' (included) and ends at the first immediately succeeding line containing 'end' (also included), and the names of the files to be written are to be result0.txt, result1.txt, and so on.
    It’s an error if the number of “closing ends” differ from that of “opening begins” (and remember, the first immediately succeeding “end” terminates all pending “begins”); no line is allowed to contain both ‘begin’ and ‘end’.

    A very arbitrary set of conditions, to be sure, but then, the OP leaves us totally in the dark about the actual specifics of the problem, so what else can we do but guess most wildly?-)

    outfile = 0
    starts = []
    ends = []
    names = []
    for i, line in enumerate(lines):
      if 'begin' in line:
        if 'end' in line:
          raise ValueError('Both begin and end: %r' % line)
        starts.append(i)
        names.append('result%d.txt' % outfile)
        outfile += 1
      elif 'end' in line:
        ends.append(i + 1)  # remember ends are EXCLUDED, hence the +1
    

    That’s it — the assert about the three lists having identical lengths will take care of checking that the constraints are respected.

    As the constraints and specs are changed, so of course will this snippet of code change accordingly — as long as it fills the three equal-length lists starts, ends, and names, exactly how it does so matters not in the least to the rest of the code.

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

Sidebar

Related Questions

I have a file which has about 25000 lines, and it's a s19 format
i have file called data_out.xml which contains this xml code: <?xml version=1.0 ?> -
I have an XML file about 1000 lines long that has a lot of
I have a file wich has about 12 millon lines, each line looks like
I have a big text file (about 100MB) and each lines are separated by
I have a binary file of size about 5MB.. which has lots of interspersed
I have always wondered about this. So many application setups have a zip file
I have a few doubts about this robots file. User-agent: * Disallow: /administrator/ Disallow:
I have a huge log file where there are about 15 million lines. I
I have a TXT file containing about 10,000 lines of text. I want to

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.