Question: I asked this question once already but it was incorrect or incomplete when answered. Please answer this question correctly. I'll pin the previous code that
I asked this question once already but it was incorrect or incomplete when answered. Please answer this question correctly. I'll pin the previous code that was given and the output im getting to avoid any mistakes or confusion
picture of me using the code that was incorrect or incomplete but previously given by one of the experts on here:
CODE ITSELF THAT WAS GIVEN:
import pwd from os import system import sys #Function to create the 4 lists def createLists(user): namesList.append(str(user.pw_name)) uIdList.append(str(user.pw_uid)) uGroupList.append(str(user.pw_gid)) uShellList.append(str(user.pw_shell))
def printUserInfo(ID): print(namesList[ID], uIdList[ID], uGroupList[ID], uShellList[ID]) # Load all user data try: allUsers = pwd.getpwall() except: print("Cannot get data!") sys.exit(1)
#Declare empty array lists namesList = [] uIdList=[] uGroupList=[] uShellList=[] #Iterate through the user data index = 0 print("USER" + "\t" + "Name") print() for user in allUsers: index=index+1 #Print the required fields print(str(index)+"\t"+str(user.pw_name)) #+ "\t"+ str(user.pw_uid) + "\t" + str(user.pw_gid) + "\t" + str(user.pw_shell)) #Call function to create lists createLists(user) #Increment Key index index=index+1
while True: print("Enter number:") input_data = input() if input_data == "exit": break numberEntered = int(input_data) printUserInfo(numberEntered)
QUESTION I NEEDED CODE FOR:
For this lab you will be accessing a file in your linux OS
File = /etc/passwd
*Create snapshot of your linux distro, so that you can go back if you make a mistake.
*This a scenario you are given, how you accomplish it with Python is up to you. Your challenge will be to logically troubleshoot through it.
Requirements (not given to you in the order they need to happen):
- Extract from each line of the file only the user name user id group id and user shell.
- Create a function that opens the file and creates 4 separate lists, incorporate error handling for your file handling.
- Create a while loop that stays open until the user types exit
- While the while loop is running the user will see on the screen all the available users on the system. The user can select a specific user and will be prompted which value they want to see (user id, group id, user shell)
REFERENCES PROBLEMS TERMINAL Python + Get Started Untitled-1 lab2.py Lab2Test.py . LAB2DTEST.py No results. > > 12 13 14 15 16 17 18 19 proxy www-data backup list irc - gnats nh vet nobody systemd-network systemd-resolve systemd-timesync more messagebus 21 3 44 syslog uuidd tcpdump kernoops 13 24 IP apt tss und avahi-autoipd usbmux rtkit 3 dnsmasq 33 cups-pk-helper speech-dispatcher Ushi avahi 35 36 canad saned 37 nm-openvpn 38 hplip 39 whoopsie 40 TO colord 41 7 geoclue 42 * pulse puise 43 T gnome-initial-setup 44 T gam 45 T sssd 46 clone TY 47 systemd-coredump Enter number: 42 Enter number: 2 Enter number: 1 Enter number: 4 Enter number: I Lab Test.py > createLists 1 import pwd 2 from os import system 3 import sys 4 #Function to create the 4 lists 5 def createLists (user): 6 namesList.append(str(user.pw_name)) 7 uIdList.append(str(user.pw_uid)) 8 uGroupList.append(str(user.pw_gid)) 9 ushellList.append(str(user.pw_shell)) 10 11 12 def printUserInfo(ID): 13 print(namesList[ID], uIdList[ID], uGroupList[ID], ushelllist[ID]). 14 # Load all user data 15 try: 16 allusers = pwd.getpwall() 17 except: 18 print("Cannot get data!") 19 sys.exit(1) 20 21 #Declare empty array lists 22 namesList = [] 23 uIdList=[] 24 uGroupList=[] 25 uShelllist=[] 26 #Iterate through the user data 27 index = 0 28 print("USER" + "\t" + "Name") 29 print() 30 for user in allusers: 31 index=index+1 32 #Print the required fields 33 print(str(index)+"\t"+str(user.pw_name)) #+ "\t"+ str(user.pw_uid) + "\t" + str(user 34 #Call function to create lists 35 createLists(user) 36 #Increment Key index 37 index=index+1 38 Ln 9. Col 42 Spaces: 4 UTF-8 :: Python 3.8.10 64-bit AO LF Pyt
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
