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) 
Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
Chad D
  • 499
  • 1
  • 10
  • 17

3 Answers3

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)
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • @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? – Chad D Jul 11 '12 at 01:08
  • 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. – David Robinson Jul 11 '12 at 01:46
  • 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] – Chad D Jul 11 '12 at 16:39
  • Could you give a reproducible example? Provide an example list and its output? Also, try the simpler version I posted above – David Robinson Jul 11 '12 at 16:56
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()
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
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().

Levon
  • 138,105
  • 33
  • 200
  • 191
  • i am just curious, what would happend if file doesnt close? – Chad D Jul 11 '12 at 00:58
  • @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: http://stackoverflow.com/questions/11398471/python-not-writing-full-string-to-file -- It *is* important you close your file(s). The nice thing about `with` is that it relieves you from having to worry about it. – Levon Jul 11 '12 at 01:02
  • 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? – Chad D Jul 11 '12 at 01:02
  • 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. – Chad D Jul 11 '12 at 01:10
  • @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. – Levon Jul 11 '12 at 01:25
  • so for example my start =10 and end =20... the result i got from the outputFile is makeList[11]...makeList[21] instead of makeList[10]...makeList[20]. Do you know how to fix this? thanks in advance – Chad D Jul 11 '12 at 01:29