0

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
  • 1
    After faking a input file that actually ends up writing a block to the output file this works fine for me. Commented Oct 11, 2009 at 8:53
  • You can use "if j in ('stem', 'differetiation', 'differetiating'):" in the second if block, it's more clear. Commented Oct 11, 2009 at 11:18

1 Answer 1

2

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.

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

7 Comments

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:)
In that case it's not easily possible to tell what's wrong without seeing from input file.
One guess anyway, maybe you meant block[0] instead of block[1]?
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.
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.
|

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.