tags:

views:

41

answers:

2

I have a simple function that sorts a dictionary:

data = inputfile.readlines()
lineData = sorted(data, key=len, reverse=True)[:3]

Printing the output:

print sorted(data, key=len, reverse=True)[:3]

generates the expected result, however writing to file:

outputfile.writelines sorted(data, key=len, reverse=True)[:3]

generates nothing. How can I write the output to the text file (outputfile)? The complete code is as follows:

import sys, string
inputfilenames, outputfilename = sys.argv[1:-1], sys.argv[-1]


def do_something_with_input(inputfile):
    data = inputfile.readlines()
    print sorted(data, key=len, reverse=True)[:3]
    print sys.path[0]+"/"+ outputfilename


def write_results(outputfile):
    data = inputfile.readlines()
    outputfile.writelines(sorted(data, key=len, reverse=True)[:3])

for inputfilename in inputfilenames:
    inputfile = open(inputfilename, "r")
    do_something_with_input(inputfile)
    outputfile = open(outputfilename, "w")
    write_results(outputfile)
+1  A: 

writelines is a method, you need to call it:

outputfile.writelines(sorted(data, key=len, reverse=True)[:3])

ETA
Function open provides file handle which could be iterated over once. You do it in your do_something_with_input function, after the inputfile iterated over, iterator is exhausted. Which means any further iterations, such as done in your write_results functions would yield an empty sequence. That's why nothing is written to the output file. Basically, it is equivalent to:

>>> a = (i for i in range(2))
>>> list(a)
[0, 1]
>>> list(a)
[]

What you need to do is store the output of the sorted(...) and then write it to the file, not try to generate it again.

SilentGhost
Thank you. Modifying the write_results function was exactly what I needed to do.
kjarsenal
A: 

You write:

outputfile.writelines sorted(data, key=len, reverse=True)[:3]

generates nothing.

it should actually generate a syntax-error exception because of the missing parentheses -- if it doesn't, I guess you must be using some (allegedly) "smart" IDE like iPython which puts in the parentheses on your behalf -- is that the case?

You're printing the three shortest lines, and they well may all be empty -- that should put three empty lines in your output file (and of course show nothing to stdout -- is that what you mean by "generates nothing"?). Maybe you're not properly calling outputfile.close() so by the time you check the file they're still buffered and not written to disk yet.

As you see there are a huge number of possibilities around your very ambiguous "generates nothing" assertion. Can you clarify exactly what environment you're using and how in particular you're checking what your code "generates" or doesn't? Otherwise, it's very hard to help you much.

Edit: the OP clarified and showed his code -- and the problem is now clear: he's totally consuming inputfile the first time, never "rewinding" it, so the file immediately ends (no lines left in it) at the second readlines call. If you do need to read the file twice independently (rather than just reading it once and passing the data around, as would be normal), you'll need a call inputfile.seek(0) to "rewind the file" each time you read it to make it ready to be read all over again.

Alex Martelli
Sorry. What I should have said was generates an empty text file. The entry has been edited to provide the complete code. I'm using IDLE.
kjarsenal
Also, it should be calling the three longest lines. The printed output does so.
kjarsenal
@kjarsenal, now that you show your code the problem has become clear, see my answer's edit.
Alex Martelli
Thank you Alex. Your instruction is greatly appreciated.
kjarsenal