0

I have a file name exclusionlist.txt and i have contents in it like

          import os
          import re
          import subprocess
          ......and many more

I have another file named libraries.txt and the contents of this file are

         import mymodule
         import empmodule,os
         import subprocess
         import datetime,logging,re
         .......and many more

My question is that from python how to know that the contents which are in exclusionlist.txt is also present in libraries.txt since here it is jumbled up..

         f = open('exclusionlist.txt', 'r')
         f.read()

         f1= open('libraries.txt', 'r')
         f1.read()

        if (//Is contents of f1 present in f2):
             print libraries found
        else:
             print not found

        f.close()
        f1.close() 
1
  • If your goal here is to stop python code from loading certain modules (i.e. run code in a sandbox environment) then you might want to have a look at the rexec module, which provides (with caveats) a "restricted execution framework". Commented Jun 4, 2011 at 7:27

1 Answer 1

1

Use set intersection:

def readImports(path):
    with open(path) as f:
        for line in f:
            # lines of form "import ___,___"
            # assuming not of form "from ___ import ___ [as ___]"
            if 'import' in line:
                modules = line.split('import')[1]
                for module in modules.split(','):
                    yield module.strip()

linesInExclusion = set(readImports('exclusionlist.txt'))
linesInLibraries = set(readImports('libraries.txt'))

print(linesInExclusion.intersection(linesInLibraries))

You can do return (line.strip() for line in f if line.strip()!='') if you want to be perfect...

Sign up to request clarification or add additional context in comments.

6 Comments

I think you want intersection (contents in both, no?) and the above won't catch the duplication of "re", as it's not a duplication of the line "import re". I think he needs to split the rhs and then intersect the sets.
@DSM oops thanks, I meant intersection, it's late... and you are also right, updating...
whats in both: linesInExclusion&linesInLibraries, whats in linesInExclusion but not in linesInLibraries: linesInExclusion-linesInLibraries, similarly: linesInLibraries-linesInLibraries, whats not in both: linesInExclusion^linesInLibraries
This is good. I think you need to yield module.strip() to deal with line-endings and other whitespace, though.
What robert king means, is that there is syntax for this (I chose not to use it for clarity). The syntax is: intersection:&, difference:-, symmetricdifference:^
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.