0
one = open('one.txt') 
two=open('two.txt')

def bres(obj1,obj2):
 x=obj1.readline().split()
 cc=obj2.readline().split()
 ela=set(x)
 elamou=set(cc)
 print elamou|ela
bres(one,two)

the file one.txt is like this: one two tree the file two.txt is like this: one four

I want to find the union of the two files. The result of this piece of code is a blank set . The correct answer is one. Where is the error?

george mano
  • 5,948
  • 6
  • 33
  • 43

3 Answers3

2

I found it !

def bres():
     with open('one.txt') as f:
      x=f.readline().split()
     with open('two.txt') as f:
      cc=f.readline().split()
      ela=set(x)
      elamou=set(cc)
      print elamou&ela
bres()
george mano
  • 5,948
  • 6
  • 33
  • 43
  • Is it a good impementation to have 2 times `with `. Could you suggest me something better? – george mano Oct 21 '12 at 03:23
  • @MarkReed According to the `function` issue you are right, there is no point of having a function. According to the `with open`, I have to use the files. That is why I use it this way. – george mano Oct 21 '12 at 03:28
  • @MarkReed Is there a way to compare the values of the file beggining from the second item? – george mano Oct 21 '12 at 03:32
  • Alternatively, you could do `with open('one.txt') as f, open('two.txt') as g:`, but I'll admit it's not necessary here because you don't need both files open at the same time. – Joel Cornett Oct 21 '12 at 03:49
  • @BurhanKhalid I mean the first item of every line. – george mano Oct 21 '12 at 04:21
1

As a matter of style, I would pass the filenames into bres, and have it return a list which you then print out from the caller.

def bres(file1, file2):
  sets = []
  for n in (file1, file2):
    with open(n) as f:
      sets.append( set( f.readline().split() ) )
  return list(sets[0] & sets[1])

print ' '.join(bres('one.txt', 'two.txt'))

Using with as above (and as you did) is the cleanest and most explicit way to deal with opening and reading the files. If, however, you are interested in shorter code for some reason, and you don't mind relying on garbage collection to close the file handles for you, you can shorten the creation of the set list to one line using a list comprehension:

def bres(file1, file2):
  sets = [ set( file(f).readline().split() ) for f in (file1, file2) ]
  return list(sets[0] & sets[1])

print ' '.join(bres('one.txt', 'two.txt'))

I was tempted to import operator and use reduce(operator.and_, sets) to generalize things - those explicit indexes just bug me. But that would probably be filed under "reasons Guido dislikes functional programming"...

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • just wondering why file() is used in the 2nd example compared to open() in the 2nd? ... otherwise, very good answer – nehz Oct 21 '12 at 04:14
  • Lisp background showing through, I guess, but `open` sounds right to me after `with`. On the other hand, `file` seems like the way to "cast a string into" a file object that you then call methods on. Technically, of course, these are exactly the same thing. But the subtle difference in context makes me reach for different words. – Mark Reed Oct 21 '12 at 04:57
  • found a SO answer: http://stackoverflow.com/questions/112970/python-when-to-use-file-vs-open – nehz Oct 21 '12 at 05:36
0
def bres(obj1,obj2):
    x=obj1.readline().split()
    cc=obj2.readline().split()
    results=[]
    for a in x:
        if a in cc:
            results.append(a)
    return results

The above should work, and is based off of what code you gave.

To get the results, all you need to do is use print bres(file_object_1,file_object_2).

IT Ninja
  • 6,174
  • 10
  • 42
  • 65