sathish kumar
sathish kumar

Reputation: 11

How can i fix this File "<string>" error in python

I trying to do the simple script and its throwing the below error at for loop,

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:

Please let me know the reason for this.

Here you can find my code,

  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'

Upvotes: 1

Views: 7169

Answers (1)

armstrhb
armstrhb

Reputation: 4152

It looks like you want to iterate over each line in the file. The open method in Python returns a file object. If you want to iterate over each line in the file, you'll need to call readlines to retrieve the contents of the file, and then loop over that.

This should work:

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'

Upvotes: 1

Related Questions