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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:54:11+00:00 2026-06-18T09:54:11+00:00

I have an issue which has to do with file input and output in

  • 0

I have an issue which has to do with file input and output in Python (it’s a continuation from this question: how to extract specific lines from a data file, which has been solved now).

So I have one big file, danish.train, and eleven small files (called danish.test.part-01 and so on), each of them containing a different selection of the data from the danish.train file. Now, for each of the eleven files, I want to create an accompanying file that complements them. This means that for each small file, I want to create a file that contains the contents of danish.train minus the part that is already in the small file.

What I’ve come up with so far is this:

trainFile = open("danish.train")

for file_number in range(1,12):
    input = open('danish.test.part-%02d' % file_number, 'r')

    for line in trainFile:
        if line not in input:
            with open('danish.train.part-%02d' % file_number, 'a+') as myfile:
                myfile.write(line)

The problem is that this code only gives output for file_number 1, although I have a loop from 1-11. If I change the range, for example to in range(2,3), I get an output danish.train.part-02, but this output contains a copy of the whole danish.train without leaving out the contents of the file danish.test.part-02, as I wanted.

I suspect that these issues may have something to do with me not completely understanding the with... as operator, but I’m not sure. Any help would be greatly appreciated.

  • 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-18T09:54:12+00:00Added an answer on June 18, 2026 at 9:54 am

    When you open a file, it returns an iterator through the lines of the file. This is nice, in that it lets you go through the file, one line at a time, without keeping the whole file into memory at once. In your case, it leads to a problem, in that you need to iterate through the file multiple times.

    Instead, you can read the full training file into memory, and go through it multiple times:

    with open("danish.train", 'r') as f:
        train_lines = f.readlines()
    
    for file_number in range(1, 12):
        with open("danish.test.part-%02d" % file_number, 'r') as f:
            test_lines = set(f)
        with open("danish.train.part-%02d" % file_number, 'w') as g:
            g.writelines(line for line in train_lines if line not in test_lines)
    

    I’ve simplified the logic a little bit, as well. If you don’t care about the order of the lines, you could also consider reading the training lines into a set, and then just use set operations instead of the generator expression I used in the final line.

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

Sidebar

Related Questions

I have input data coming from a flat file which has english, japanese, chinese
I have an issue with log4net which has been bugging me for a while
I'm pretty new in iphone programming and have stumbled upon this issue which I
I have a form on which I use validation. This form has a section
I have a minified/packed javascript file which is causing problems. The issue is that
I have to create a CSV file from webservice output and the CSV file
I have a jquery-ui-dialog that contains a <form> which contains a <input type=file >
I have an express server that has a button that issues a POST which
I have an issue which I could not find answer for across the web.
I have some urgent issue which I could not find answer for across the

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.