0

I'm trying to open a text file called filteredApps.txt which contains "app1.ear, app2.ear, app3.ear, app4.ear" as new lines, pass it to a list and then compare it to another list. Then finally call the deploy function in the main() method but I get AttributeError: getitem in the line hightlighted below on the code:

appNames = ['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']

def filteredApps():
    filteredAppsList = []
    appToDeploy = open("filteredApps.txt","r")
    for deploy in appToDeploy:   #Code breaks here
        filteredAppsList.append(deploy)
    return map(str.strip, filteredAppsList)

def main():
    finalListToDeploy = []
    listToDeploy = filteredApps() #Code breaks here as well

    for paths in appNames:
        for apps in listToDeploy:
            if apps in paths:
                finalListToDeploy.append(apps)
    deployApplication(finalListToDeploy)

if __name__ == "__main__":
    main()
15
  • What does Code breaks here, mean? What error do you get? Commented Apr 9, 2019 at 7:39
  • Please post the complete trace back. Commented Apr 9, 2019 at 7:39
  • Where is deployApplication defined? Commented Apr 9, 2019 at 7:39
  • appToDeploy = open("filteredApps.txt","r") not the right way to do it Commented Apr 9, 2019 at 7:41
  • I'm just highlighting which lines jenkins says the error comes from. The code breaks before the deployApplication() is called so it is not relevant for this question Commented Apr 9, 2019 at 7:42

3 Answers 3

1

Continuing from the comments:

filteredApps.txt:

app1
app2
app3
app4

Hence:

appNames = ['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']

def filteredApps():
    filteredAppsList = []
    with open("filteredApps.txt","r") as appToDeploy:
      for apptodeploy in appToDeploy:
          # print(apptodeploy)
          filteredAppsList.append(apptodeploy)
    return map(str.strip, filteredAppsList)

def main():
    finalListToDeploy = []
    listToDeploy = list(filteredApps())
    for paths in appNames:
        for apps in listToDeploy:
            if apps in paths:
                # print(paths)
                finalListToDeploy.append(paths)
    return finalListToDeploy
    # deployApplication(finalListToDeploy)

if __name__ == "__main__":
    print(main())

OUTPUT:

['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use open as follow:

import io
from io import open
with open('tfilteredApps.txt', 'r', encoding='utf-8') as file :    
    for deploy in file :
        filteredAppsList.append(deploy)

But if you have all app name in one line it gonna be like this with pickle module:

import pickle
with open('tfilteredApps.txt', 'r', encoding='utf-8') as file :
    word = pickle.load(file)
filteredAppsList = word.split(' ')

1 Comment

Thanks! I will try it soon and give feedback
0

Try reading the file before looping over the data

 appToDeploy = open("filteredApps.txt","r") 
 contents = appToDeploy.readlines()
 appToDeploy.close()

 for deploy in contents:     
     filteredAppsList.append(deploy)

4 Comments

Thanks. I'm busy testing it now. Give me a sec
@Sizwe how's it going? Any advances?
09:53:05 Traceback (innermost last): 09:53:05 File "<string>", line 129, in ? 09:53:05 File "/home/jenkins/workspace/sizwe_deploy/bin/../deploy/deployApplication.py", line 350, in main 09:53:05 File "/home/jenkins/workspace/sizwe_deploy/bin/../deploy/deployApplication.py", line 329, in filteredApps 09:53:05 AttributeError: instance of 'org.python.core.PyReflectedFunction' has no attribute 'strip'
@Sizwe that's got something to do with your map(str.strip, filteredAppsList)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.