snippetpythonMinor
Create CSV file of domain groups and their users
Viewed 0 times
groupsfilecreatecsvandusersdomaintheir
Problem
I'm fairly new to scripting in Python. I'm looking for suggestions on improving any of the following in the below script: my coding style, performance, readability, commenting, or anything else.
"""
META
Author: James Nicholson
Date: 8/16/2013
DESCRIPTION
This script takes a list of groups from a text file and creates a new text file
in CSV format that contains group members in the below format:
group_name1,user_name1
group_name1,user_name2
etc...
VARIABLES
server = 'domain name'
glist = 'path to text file containing desired groups'
"""
# Import the win32 module needed for accessing groups and members
import win32net
# Set server name and group_list text file variables
server = # enter server name here
group_list = 'groups.txt'
grps = []
# Loop over lines in group_list and store group names in a list
with open(group_list, 'rb') as my_groups:
for line in my_groups:
grps.append(line.rstrip('\r\n'))
# Loop over groups and store members in desired format in new text file 'groups_members_output.txt'
with open('groups_members_output.txt', 'wb') as blank_file:
for group in grps:
usrs = win32net.NetGroupGetUsers(server,group,0)
for user in usrs[0]:
blank_file.write('{},{}\r\n'.format(group,user['name']))Solution
Some notes:
-
Comments: A lot (if not all) of the comments are pretty much repeating what the code already says. I'd remove them.
-
-
-
-
Put spaces after commas.
-
Use (functional) list-comprehensions expressions instead of (imperative) for-loop statements whenever possible.
-
Use always
I'd write:
-
Comments: A lot (if not all) of the comments are pretty much repeating what the code already says. I'd remove them.
-
server = # enter server name here. Create functions with configurable elements as arguments instead of global variables.-
with open('groups_members_output.txt', 'wb') as blank_file. Try to give more meaningful names to variables, be declarative. blank_file says hardly anything useful about the variable.-
grps/usrs: Just for saving one or two characters now variables are harder to read.-
Put spaces after commas.
-
Use (functional) list-comprehensions expressions instead of (imperative) for-loop statements whenever possible.
-
Use always
"\n" for line endings (read and write), Python takes care of portability in all platforms.I'd write:
import win32net
def write_group_members(server, input_groups, output_groups):
with open(input_groups) as input_groups_fd:
groups = [line.rstrip() for line in input_groups_fd]
with open(output_groups, "w") as output_groups_fd:
for group in groups:
users = win32net.NetGroupGetUsers(server, group, 0)[0]
for user in users:
contents = "{0},{1}\n".format(group, user["name"])
output_groups_fd.write(contents)
write_group_members("someserver.org", "groups.txt", "groups_members_output.txt")Code Snippets
import win32net
def write_group_members(server, input_groups, output_groups):
with open(input_groups) as input_groups_fd:
groups = [line.rstrip() for line in input_groups_fd]
with open(output_groups, "w") as output_groups_fd:
for group in groups:
users = win32net.NetGroupGetUsers(server, group, 0)[0]
for user in users:
contents = "{0},{1}\n".format(group, user["name"])
output_groups_fd.write(contents)
write_group_members("someserver.org", "groups.txt", "groups_members_output.txt")Context
StackExchange Code Review Q#29961, answer score: 3
Revisions (0)
No revisions yet.