Skip to content

Instantly share code, notes, and snippets.

@Ojha-Shashikant
Created November 30, 2018 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ojha-Shashikant/9d2ee590d93753f40d091472cb5930c4 to your computer and use it in GitHub Desktop.
Save Ojha-Shashikant/9d2ee590d93753f40d091472cb5930c4 to your computer and use it in GitHub Desktop.
Reading password file, printing output in ascending order of user id with user real name and home directory.
''' Reading password file, printing output in ascending order of user id with user real name and home directory '''
def reading_file():
with open("C:\\Users\\Ojha\\Documents\\Python exercises\\Python Scripts\\password.txt", 'r+') as FH:
text = FH.read()
return text
def file_data_operation(text):
lines = text.split('\n')
users_data = dict()
output_data = dict()
for line in lines:
data = line.split(':')
user = dict()
for idx in range(0,6):
if idx == 0:
user['username'] = data[idx]
elif idx == 2:
user['user_id'] = data[idx]
elif idx == 4:
user['user_realname'] = data[idx]
elif idx == 5:
user['home_directory'] = data[idx]
users_data[data[2]] = user
return users_data
def sorting_id(users_data):
user_id = list(users_data.keys())
user_id.sort()
for id_num in user_id:
for k,v in users_data.items():
if id_num == k:
print(v)
# main starts here
if __name__ = '__main__':
text = reading_file()
users_data = file_data_operation(text)
sorting_id(users_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment