[Tutor] Multiple file open

nitin chandra nitinchandra1 at gmail.com
Fri Aug 20 09:28:42 CEST 2010


Hello All,

I am getting the "Invalid Syntax" error. I am on Python 2.6.

Thanks In Advance

Nitin

****************************************************
import sys,os, fileinput


FileA = raw_input('Enter CSV file with lists of Files:')
try:
   fp6 = open(FileA,'r')
   except IOError:

##  I am getting the following error at this line:-
##
##    File "mer5Pr2.py", line 7
##    except IOError:
##            ^
##    SyntaxError: invalid syntax

      sys.exit('Could not open file: %s' %  FileA)

while True:
   lines = fp6.readline().split(",")
   file11 = lines[0]
   file12 = lines[1]
   file3 = lines[2]
#file11 = raw_input('Enter PR1 File name :')
#fp1 = open(file11,'r')
   try:
      fp1 = open(file11, 'r')
      except IOError:
         sys.exit('Could not open file: %s' % file11)
#file12 = raw_input('Enter PR3 File Name :')
#fp2 = open(file12,'r')
   try:
     fp2 = open(file12, 'r')
     except IOError:
        sys.exit('Could not open file: %s' % file12)

#file3 = raw_input('Enter PR2 OUTPUT File Name :')
   try:
     fp3 = open(file3,'w')
     except IOError:
        sys.exit('Could not open file %s to Write' % file3)

while True:
   try:
       line1 = fp1.readline().split(",")
       line2 = fp2.readline().split(",")
       #line1 = line1A.split(",")
       col1 = line1[0]
       col2 = line1[1]
       col3 = line1[2]
       col4 = line1[3]
       col5 = line1[20]
       col6 = line1[21]
       col7 = line1[22]
       #line2 = line1B.split(",")
       col8 = line2[1]
       col9 = line2[2]
       col10 = line2[3]
       col11 = line2[20]
       col12 = line2[21]
       col13 = line2[22]
#def __FormulaPR2():
       #claculation of PR2 as per formula
       #(A+B)/2 , ie. Mean of the two values
       col14 = (((float(col2)) + (float(col8))) / 2)
       col15 = (((float(col3)) + (float(col9))) / 2)
       col16 = (((float(col4)) + (float(col10))) / 2)
       col17 = (((float(col5)) + (float(col11))) / 2)
       col18 = (((float(col6)) + (float(col12))) / 2)
       col19 = (((float(col7)) + (float(col13))) / 2)

       print col1,col14,col15,col16,col17,col18,col19
       str3 = '%s,%s,%s,%s,%s,%s,%s\n' %
(col1,col14,col15,col16,col17,col18,col19)
       fp3.write(str3)


#def __FormulaPR4():
      #calculation of PR4 as per formula
      #(B-C)+3 , ie. Extrapolation of the values



fp1.close()
fp2.close()
fp3.close()


More information about the Tutor mailing list