Menu

(Solved) : 3 Create Educalhounpythonfinalprojectaviationutil Module Package Educalhounpythonfinalproj Q37274563 . . .

3. Create the ‘edu.calhoun.python.finalproject.aviation.util’module:

• In the package ‘edu.calhoun.python.finalproject.aviation’,create the Python module util.
• Edit the util module.
• Using a from statement do a relative import (‘.pilots’) for theclasses in the module pilots created in #2.
• Create the function readPilotsFile(fileName).
o Create an empty list with the variable name pilots.
o Create the variable fileInput with the value of None.
o Using a try statement, attempt to open the file specified by thestring in the fileName variable with read- only privileges:
▪ fileInput = open(fileName, ‘r’)
o Using an except statement that tests for an IOError, print thefollowing:
▪ print(‘Error opening the file “‘ + fileName + ‘” forreading.’)
o Using an else statement, iterate through the lines in the readfile using a for loop assigning each list item to the variablepilotRecord and do the following:
▪ Split each line (which is a string) found in the file and assignthe resulting list it to the variable pilotAttributes.
• pilotAttributes = pilotRecord.split()
▪ Create a new instance of the specific pilot class using thestring value in the first offset in in the list pilotAttributes andassign that class instance to the variable pilot:
• pilot = globals()[pilotAttributes[0]]()
▪ Using the class instance created, set the first name using thestring value in the second offset in in the listpilotAttributes
▪ Using the class instance created, set the last name using thestring value in the third offset in in the listpilotAttributes
▪ Append the new pilot class to the pilots list
o Using a finally statement, do the following:
▪ Using a try statement, attempt to close the file using thevariable fileInput:
• fileInput.close()
▪ Using an except statement, print the following:
• print(‘Error closing the file “‘ + fileName + ‘”.’)
o Using a return statement, return the list of pilots.
• Create the function writePilotsFile(fileName, pilots).
o Create the variable fileOutput with the value of None.
o Using a try statement, attempt to open the file specified by thestring in the fileName variable with write privileges:
▪ fileOutput = open(fileName, ‘w’)
o Using an except statement that tests for an IOError, print thefollowing:
▪ print(‘Error opening the file “‘ + fileName + ‘” forwriting.’)
o Using an else statement, iterate through the pilots list using afor loop assigning each list item to the variable pilot and writeeach pilot in the pilots list to the file using thefolllowing:
▪ fileOutput.write(pilot.getClassName() + ‘ ‘ +pilot.getFirstName() + ‘ ‘ + pilot.getLastName() + ‘n’)
o Using a finally statement, do the following:
▪ Using a try statement, attempt to close the file using thevariable fileOutput:
• fileOutput.close()
▪ Using an except statement, print the following:
• print(‘Error closing the file “‘ + fileName + ‘”.’)

This is what i have so far and keep getting an error: (pilot =globals()[pilotAttributes[0]]() KeyError: ‘a’)

from .pilots import Pilot

fileName = ‘pilots.dat’

def readPilotsFile(fileName):
pilots = [ ]
fileInput = None
  
try:
fileInput = open(fileName,’r’)
except (IOError):
print(‘Error opening the file “‘ + fileName + ‘” forreading.’)
else:
for pilotRecord in fileName:
pilotAttributes = pilotRecord.split()
  
pilot = globals()[pilotAttributes[0]]()
pilot.setFirstName[pilotAttributes[1]]
pilot.setLastName[pilotAttributes[2]]
finally:
try:
fileInput.close()
except (IOError):
print(‘Error closing the file “‘ + fileName + ‘”.’)
return pilots

def writePilotsFile(fileName,pilots):
fileOutput = None
try:
fileOutput = open(fileName, ‘w’)
except (IOError):
print(‘Error opening the file “‘ + fileName + ‘” forwriting.’)
else:
for pilot in pilots:
fileOutput.write(pilot.getClassName() + ‘ ‘ + pilot.getFirstName()+ ‘ ‘ + pilot.getLastName() + ‘n’)
finally:
try:
fileOutput.close()
except (IOError):
print(‘Error closing the file “‘ + fileName + ‘”.’)

Expert Answer


Answer to 3. Create the ‘edu.calhoun.python.finalproject.aviation.util’ module: • In the package ‘edu.calhoun.python.finalprojec…

OR