''' Software Tools Exam 1 Problem 1 Given a specific file format (.edf), write a parser which displays the first 10 fields of the file. Input: Any singular .edf file Output: First 10 fields of the .edf file Usage: python smith_zack_p01.py edf_file.edf ''' # Import sys library for getting command line arguments import sys # Main def main(): # Create read string variable read_string = '' # Make sure a command line argument is used try: # Check that the last four letters are '.edf' if sys.argv[1][-4:len(sys.argv[1])] != '.edf': # Return if not 'edf' file format print "Not an edf file! Please use an .edf document" return # Try to open the input file with open(sys.argv[1], 'r') as file_thing: # First 10 fields contained in first 144 bytes read_string += file_thing.read(144) # Split read string by spaces to get individual fields fields_array = read_string.split() for field_iterator in range(len(fields_array)): print 'Field %d: %s' % (field_iterator+1, fields_array[field_iterator]) # If you can't get sys.argv[1], print an error except IndexError: print "Please enter an .edf file as a command line argument" return # Boiler plate code if __name__ == '__main__': main()