-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_username.py
More file actions
41 lines (29 loc) · 797 Bytes
/
read_username.py
File metadata and controls
41 lines (29 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'''
Author : Ashutosh Kumar
PRN : 19030142009
Assignment: Read all the usernames from the /etc/passwd file
'''
new_file,fp=""
try:
#opening file in read mode
fp=open('/etc/passwd','r')
#creating new file for usernames
new_file=open('usernames.txt','w')
#extracting every user from the file
for line in fp:
#writing it to the new file
new_file.write(line.split(':')[0]+'\n')
except FileNotFoundError:
print("File not Found ")
except FileExistsError:
print("File Already Exists")
except PermissionError:
print("Permissison to open file is not granted ")
except IsADirectoryError:
print("It is a directory")
finally:
try:
new_file.close();
fp.close()
except AttributeError:
print("File not opened ")