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()
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']
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)
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(' ')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.