1

My goal is to print out the index of makeList on to another file. I have check my start and end values, which came out correct. However, my outputFile is totally off because it only print one character on that file.

def printOutput(start, end, makeList):
  if start == end == None:
      return
  else:
      while start <= end:
          print start
          print end
          outputFile = open('out'+uniprotID+'.txt','w')#file for result output
          inRange = makeList[start]
          start += 1
          outputFile.write(inRange) 

3 Answers 3

2

Move the line:

outputFile = open('out'+uniprotID+'.txt','w')#file for result output

to the line before the while loop. Right now it is reopening the file (as a completely new, empty file) on every single iteration of the while loop.

So the code would be:

def printOutput(start, end, makeList):
  if start == end == None:
      return
  else:
      outputFile = open('out'+uniprotID+'.txt','w')#file for result output
      while start <= end:
          print start
          print end
          inRange = makeList[start]
          start += 1
          outputFile.write(inRange) 

ETA: There is a much easier way to do this using list slicing:

def printOutput(start, end, makeList):
  if start == end == None:
      return
  else:
      outputFile = open('out'+uniprotID+'.txt','w')#file for result output
      for inRange in makeList[start:end+1]:
          outputFile.write(inRange)
Sign up to request clarification or add additional context in comments.

4 Comments

@DavidRobinson I check makeList index value and output file, it didnt came out right. I print index of make list +1 of the actual value of the index even when i switch the order of outputFile.write(inRange) and start +=1. do you know why is this happen?
In what way did it not come out right- what was the output? For one thing, you won't have a newline separating each of the values in the output unless you change the code to outputFile.write(inRange + "\n"). Also, see my edit for a much simpler version of the code.
so for example my start value is 10 and end value is 20. However in the outputfile it print makeList[11]makeList[12]...until makeList[21]
Could you give a reproducible example? Provide an example list and its output? Also, try the simpler version I posted above
0

This is happening because you open the file for writing several times. Basically, this makes the program overwrite the file in each iteration of the while loop.

To minimally modify your code, open your file with the 'a' flag instead of the 'w' flag. This opens the file in append mode instead of overwrite mode.

However, this will make your code repeatedly open the file, which will cause it to slow down as disk I/O operations take time. Instead, a better way to do this would be to open the file for writing outside the while loop and just write to it inside. In code:

def printOutput(start, end, makeList):
    if start == end == None:
        return
    else:
        outputFile = open('out'+uniprotID+'.txt','w')#file for result output
        while start <= end:
            print start
            print end
            inRange = makeList[start]
            start += 1
            outputFile.write(inRange)
        outputFile.close()

Comments

0

The problem, as already stated, was that the output file was being repeatedly opened inside the loop. The fix is to open the output file before you enter the loop.

Here's one more version, using with to open your file. The advantage of using this construct is that it will automatically close your file for you when you are done, or if you encounter an exception.

   def printOutput(start, end, makeList):
      if start == end == None:
          return
      else:
          out_fname = 'out'+uniprotID+'.txt'
          with open(out_fname, 'w') as outputFile:
              while start <= end:
                  print start
                  print end
                  inRange = makeList[start]
                  start += 1
                  outputFile.write(inRange) 

Otherwise, you would have to remember to explicitly close the file with outputFile.close().

6 Comments

i am just curious, what would happend if file doesnt close?
@ChadD In some instances all of your data won't get written to the output file. Your output is collected in a buffer before it gets written to a file. If your program doesn't close the file, this buffer may not get flushed. Here is a recent problem on SO dealing with just this: stackoverflow.com/questions/11398471/… -- It is important you close your file(s). The nice thing about with is that it relieves you from having to worry about it.
thank you very much it works. is there a way that i can have the print out of index number that makeList print to my outputfile on the console?
yes, i switched the order of outputFile.write(inRange) and start +=1. However, it still print makeList of index +1 of the actual value. I don't understand why would this happen.
@ChadD I am not sure I understand your question, but you can still use print inside your loop as before to display values to the console. Writing to a file at the same time does not prevent you from doing this.
|

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.