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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:41:01+00:00 2026-06-04T01:41:01+00:00

I have a piece of code to read two files, convert them to sets,

  • 0

I have a piece of code to read two files, convert them to sets, and then subtract one set from the other. I would like to use a string variable (installedPackages) for “a” instead of a file. I would also like to write to a variable for “c”.

a = open("/home/user/packages1.txt")
b = open("/home/user/packages.txt")
c = open("/home/user/unique.txt", "w")

for line in set(a) - set(b):
    c.write(line)

a.close()
b.close()
c.close()

I have tried the following and it does not work:

for line in set(installedPackages) - set(b):

I have tried to use StringIO, but I think I am using it improperly.

Here, finally, is how I have created installedPackages:

stdout, stderr = p.communicate()
installedPackages = re.sub('\n$', '', re.sub('install$', '', re.sub('\t', '', stdout), 0,re.MULTILINE))

Sample of packages.txt:

humanity-icon-theme
hunspell-en-us
hwdata
hyphen-en-us
ibus
ibus-gtk
ibus-gtk3
ibus-pinyin
ibus-pinyin-db-android
ibus-table
  • 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-04T01:41:03+00:00Added an answer on June 4, 2026 at 1:41 am

    Something like the following?

    Edit: after several iterations:

    from subprocess import Popen, PIPE
    
    DEBUG = True
    if DEBUG:
        def log(msg, data):
            print(msg)
            print(repr(data))
    else:
        def log(msg, data):
            pass
    
    def setFromFile(fname):
        with open(fname) as inf:
            return set(ln.strip() for ln in inf)
    
    def setFromString(s):
        return set(ln.strip() for ln in s.split("\n"))
    
    def main():
        # get list of installed packages
        p = Popen(['dpkg', '--get-selections'], stdout=PIPE, stderr=PIPE)
        stdout, stderr = p.communicate()
        installed_packages = setFromString(stdout)
    
        # get list of expected packages
        known_packages = setFromFile('/home/john/packages.txt')
    
        # calculate the difference
        unknown_packages = installed_packages - known_packages
        unknown_packages_string = "\n".join(unknown_packages)
    
        log("Installed packages:", installed_packages)
        log("Known packages:", known_packages)
        log("Unknown packages:", unknown_packages)
    
    if __name__=="__main__":
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the following piece of code to read data from files as part
So, I have this piece of code, which will just read the message from
I have a piece of JS code to generate random numbers and output them
I have a mostly-working piece of code to read a directory, output each folder
I have a piece of code that reads the content from a non-empty InputStream.
I have piece of JavaScript code which cycles through form elements and builds an
I have a piece of code written in PHP inside my javascript tag. I
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
I have a piece of code looking like this : TAxis *axis = 0;
I have a piece of code here that i really could use some help

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.