tags:

views:

92

answers:

1

I've got a weird problem with python programming. I used the statement'writelines()' to write a series of lists into a new file.During the process I could see the context in the file icon via preview, however once after the program finished running the output file comes out to be a blank file.

In short, my problem is that the program doesn't have any output but a blank file.

Here's the code:

infile=open('/Users/Jim/Desktop/py/result.txt','r')
outfile=open('/Users/Jim/Desktop/py/output.txt','a')
line=infile.readline()
i=1
while(line):
    check=str(i)+':'
    if line.startswith(check):
     i+=1
     block=[infile.readline() for j in range(1,7)]
     judge=block[1].split()
     for j in judge:
      if j=='stem' or j=='differetiation' or j=='differetiating':
       outfile.write(str(i)+':\n')
       outfile.writelines(block)    #check if the paragraph has the given key words, if true then write the paragraph into the output file.
       break
    line=infile.readline()
outfile.close()
infile.close()

Some additional information if helpful: The python version is 2.6.3, and the os is Mac OS 10.6.

+2  A: 

I guess it's caused by incorrect indentation. The break statement should be inside the if block. The loop as it is written will only try the first option from judge. Check if you don't have mixed spaces and tabs in the file.

Lukáš Lalinský
I had a mistake when posting the code to web, actually the break does belong to the if block. I fixed this mistake and now the code is as it is in my mac, and the problem still exists. Thanks for the answer anyway:)
Jim
In that case it's not easily possible to tell what's wrong without seeing from input file.
Lukáš Lalinský
One guess anyway, maybe you meant `block[0]` instead of `block[1]`?
Lukáš Lalinský
Well, the input file is a result output from a query in NCBI. The content is some pure text. About the 'block[0]', I'm pretty sure it's good. In fact, the output file does have some text during the process of the program running, however once after the program ends the file just turns to blank automatically. I just wonder what makes this happen.
Jim
Well, it's you who wanted help debugging the script. :) You should make it easy for people to help you. This way I can only recommend you debug print statements or tracing the script in a debugger.
Lukáš Lalinský
Thanks anyway:) I'm just too confused about the problem. Maybe I should think over it for some time and then come here to clarify my question.
Jim
Maybe, but the previous comment was meant as a hint. If you include a sample from the input file in your question, you will get a much higher change of having the question correctly answered.
Lukáš Lalinský