我正在用Python编写一个脚本,它将查看linux系统上的所有组和所有用户,并向我输出一个文件。在
我有一种情况,如果某个帐户在某个组中,我想自己设置用户ID(在本例中,这是因为该帐户是非用户帐户)
代码如下:#/usr/bin/python
import grp,pwd,os
from os.path import expanduser
destdir = expanduser("~")
destfile = '/newfile.txt'
appname = 'app name'
groupid = ''
userid = ''
#delete old feed file and create file
if os.path.exists(destdir + destfile):
os.remove(destdir + destfile)
print "file deleted...creating new file"
output = open(destdir + destfile, 'w+')
output.write('ACCOUNTID|USERLINKID|APPLICATIONROLE|APPLICATION' + '\n')
else:
print "no file to delete...creating file"
output = open(destdir + destfile, 'w+')
output.write('ACCOUNTID|USERLINKID|APPLICATIONROLE|APPLICATION' + '\n')
#get user/group data for all users non primary groups
#documentation: https://docs.python.org/2/library/grp.html
groups = grp.getgrall()
for group in groups:
groupid = group[2]
print groupid #checking to see if group ids print correctly. Yes it does
for user in group[3]:
if groupid == '33': #Issue is here!
userid = 'qwerty'
print userid #testing var
output.write(user + '|' + userid + '|' + group[0] + '|' + appname + '\n')
问题是:
^{pr2}$
变量“userid”从未设置为它的值,并且在测试时从不打印任何内容。在
组“33”中确实有用户并且存在。我不明白为什么这样不行:(
我有另一段代码可以为用户实现这一点(因为我正在查看主要组和次要组,一旦我弄清楚这一部分,我就可以修复其余部分)