Tuesday, April 3, 2012

Check if files in a list exist with Python

I needed to test whether a large list of galaxies have corresponding mask files in a certain directory. The tentative mask filenames were created by a code that cross-correlated internal survey galaxy IDs with their respective NED names.

  wrongFilenames = []
  for i in range(0, noOfGalaxies):
    maskFile = GalaxyParameters.getMaskUrl(listFile, dataDir, simpleFile, i)
    print i, 'maskfile: ', maskFile  
    try:
      f = open(maskFile)
      print "maskfile exists:", maskFile
    except IOError as e:
      wrongFilenames.append(maskFile)

No comments:

Post a Comment