1

我试图做一个简单的脚本,并在 for 循环中抛出以下错误,

WASX7017E: Exception received while running file "/abc/websphere/wasad/createusers.py"; 
exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
File "<string>", line 22, in ?
AttributeError: __getitem__

filename=sys.argv[0]
file_read= open( filename)   ---- this is line 22
for row in file_read:

请让我知道原因。

在这里你可以找到我的代码,

  import sys

  filename="/usr/websphere/onefolder/Userlist.txt"
  fileread = open(filename, 'r')
  for row in fileread:
     column=row.strip().split(';')
     user_name=column[0]
     pass_word=column[1]
     AdminTask.createUser(['-uid',user_name, '-password', pass_word, '-confirmPassword',   pass_word])
     AdminTask.mapUsersToAdminRole(['-roleName','Administrator','-userids',user_name])
     AdminTask.addMemberToGroup('[-memberUniqueName user_name,o=defaultWIMFileBasedRealm -groupUniqueName cn=webarch,o=defaultWIMFileBasedRealm]')

  fileread.close()

  AdminConfig.save()

  print 'Saving Configuration is completed'
4

1 回答 1

1

看起来您想要遍历文件中的每一行。openPython 中的方法返回一个文件对象。如果要遍历文件中的每一行,则需要调用readlines以检索文件的内容,然后循环遍历该文件。

这应该有效:

import sys

filename="/usr/websphere/onefolder/Userlist.txt"
fileread = open(filename, 'r')

filelines = fileread.readlines()  

for row in filelines:
   column=row.strip().split(';')
   user_name=column[0]
   pass_word=column[1]
   AdminTask.createUser(['-uid',user_name, '-password', pass_word, '-confirmPassword',   pass_word])
   AdminTask.mapUsersToAdminRole(['-roleName','Administrator','-userids',user_name])
   AdminTask.addMemberToGroup('[-memberUniqueName user_name,o=defaultWIMFileBasedRealm -groupUniqueName cn=webarch,o=defaultWIMFileBasedRealm]')

fileread.close()

AdminConfig.save()

print 'Saving Configuration is completed'
于 2013-02-01T17:40:07.717 回答