Start New Discussion within our Software Development Community

I have a source code that opens a csv file and sets up a header to
value association. The source code is given below:

def ParseCsvFile(source):
  """Parse the csv file.

  Args:
    source: file to be parsed.
    Returns: the list of dictionary entities; each dictionary contains
attribute
                 to value mapping or its equivalent.
  """
  global rack_file
  rack_type_file = None
  try:
    rack_file = source
    rack_type_file = open(rack_file)
    headers = rack_type_file.readline().split(',')
    length = len(headers)
    reader = csv.reader(rack_type_file, delimiter=',')
    attributes_list=[] # list of dictionaries.
    for line in reader:
      # More process to happeng. Converting the rack name to sequence.
      attributes_list.append(dict((headers[i], line[i]) for i in
range(length)))
    return attributes_list
  except IOError, (errno, strerror):
    logging.error("I/O error(%s): %s" % (errno, strerror))
  except IndexError, (errno, strerror):
    logging.error('Index Error(%s), %s' %(errno, strerror))
  finally:
    rack_type_file.close()

I am trying to mock the following statement

rack_type_file = open(rack_file)

How do I mock open(...) function? Any assistance would be appreciated

This question has already been answered. Start a new discussion instead.