Created
June 13, 2010 13:16
-
-
Save abhiomkar/436648 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # Title: Update Employee records in a plain text file | |
| FILE_NAME = 'employee.txt' | |
| DELIMIT = ';' | |
| def prompt_values(): | |
| _values = {} | |
| _values['ename'] = raw_input("Employee Name: ") | |
| _values['esal'] = float(raw_input("Employee Sal: ")) | |
| return _values | |
| def main(): | |
| print "Enter your option:" | |
| print "1. Insert a new employee record" | |
| print "2. Update a existing record" | |
| print "3. Delete a record" | |
| print ": " | |
| opt = raw_input().strip() | |
| if opt == '1': | |
| _empId = int(raw_input("Employee ID: ")) | |
| records = open(FILE_NAME).readlines() | |
| for rec in records: | |
| if rec.split(DELIMIT)[0] == _empId: | |
| # record is already existing... | |
| print "This record already exists" | |
| print rec.split(DELIMIT) | |
| records.close() | |
| return | |
| efile = open(FILE_NAME, 'a+') | |
| erecord = prompt_values() | |
| efile.write("%s;%s;%s" % (_empId, erecord['ename'], erecord['esal'])) | |
| efile.write("\r\n") | |
| efile.close() | |
| return | |
| elif opt == '2': | |
| _empId = int(raw_input("Employee ID: ")) | |
| records = open(FILE_NAME).readlines() | |
| wrecords = [] | |
| for rec in records: | |
| if rec.split(DELIMIT)[0] == _empId: | |
| # record found... | |
| erecord = prompt_values() | |
| wrecords.append(_empId, erecord['ename'], erecord['esal']) | |
| print "Record is updated" | |
| else: | |
| wrecords.append(rec) | |
| efile = open(FILE_NAME, 'w') | |
| efile.writelines(wrecords) | |
| efile.close() | |
| elif opt == '3': | |
| pass | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment