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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:18:10+00:00 2026-06-16T05:18:10+00:00

I have the following function. The program looks at each file and prints the

  • 0

I have the following function. The program looks at each file and prints the lines occuring in all 4 files to a new file. I’ve tried file1.close() but I get an error about closing a set? I think I could use a with statement but not sure how to do this, I’m very new to programming.

def secretome():
    file1 = set(line.strip() for line in open(path + "goodlistSigP.txt"))
    file2 = set(line.strip() for line in open(path + "tmhmmGoodlist.txt"))
    file3 = set(line.strip() for line in open(path + "targetpGoodlist.txt"))
    file4 = set(line.strip() for line in open(path + "wolfPsortGoodlist.txt"))
    newfile = open(path + "secretome_pass.txt", "w")
    for line in file1 & file2 & file3 & file4:
        if line:
            newfile.write(line + '\n')
    newfile.close()
  • 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-16T05:18:12+00:00Added an answer on June 16, 2026 at 5:18 am

    I would suggest removing the repetition by extracting your set generation into a function:

    def set_from_file(path):
        with open(path) as file:
            return set(lines.strip() for line in file)
    
    def secretome():
        files = ["goodlistSigP.txt", "tmhmmGoodlist.txt", "targetpGoodlist.txt", "wolfPsortGoodlist.txt"]
        data = [set_from_file(os.path.join(path, file)) for file in files]
        with open(path + "secretome_pass.text", "w") as newfile:
            newfile.writelines(line + "/n" for line in set.union(*data) if line)
    

    Note that you are doing intersection in your code, but you talk about wanting a union, so I used union() here. There are also a couple of list comprehensions/generator expressions.

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

Sidebar

Related Questions

In my program I have coded the following private variables and function. private string
I have a program that is creating multiple files. There is a function for
I have following function in one Activity public void AppExit() { Editor edit =
In my MVC application in Controller i have following function to add and focus
have the following function on my collection : getFiltered: function (status, city) { return
I have the following function set up to sequentially fade in divs (with class
I have the following function to remove a DOM element div, $('#emDiv').on(click, ':button[data-emp-del=true]', function
I have the following function //simple function with parameters and variable function thirdfunction(a,b,c,d){ console.log(the
I have the following function: func fitrange(a, x, b int) int { if a
I have the following function in a PHP script, which returns and API call:

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.