我该如何解决这个文件&#34; <string>&#34; python中的错误</string>

时间:2013-01-30 00:12:56

标签: python websphere jython wsadmin

我尝试做简单的脚本,并在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'

1 个答案:

答案 0 :(得分:1)

看起来你想迭代文件中的每一行。 Python中的open方法返回一个文件对象。如果要遍历文件中的每一行,则需要调用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'