AttributeError: 'tuple' object has no attribute 'append' [duplicate]
Asked Answered
P

4

8

Can anyone help me with this code?

Jobs = ()
openFile = open('Jobs.txt')
x = 1
while x != 0:
    Stuff = openFile.readline(x)
    if Stuff != '':
        Jobs.append(Stuff)
    else:
        x = 0

This code throws:

AttributeError: 'tuple' object has no attribute 'append'

I'm using Python 3.6.

Perm answered 1/2, 2017 at 17:28 Comment(0)
T
9

In the line:

Jobs = ()

you create a tuple. A tuple is immutable and has no methods to add, remove or alter elements. You probably wanted to create a list (lists have an .append-method). To create a list use the square brackets instead of round ones:

Jobs = []

or use the list-"constructor":

Jobs = list()

However some suggestions for your code:

opening a file requires that you close it again. Otherwise Python will keep the file handle as long as it is running. To make it easier there is a context manager for this:

with open('Jobs.txt') as openFile:
    x = 1
    while x != 0:
        Stuff = openFile.readline(x)
        if Stuff != '':
            Jobs.append(Stuff)
        else:
            x = 0

As soon as the context manager finishes the file will be closed automatically, even if an exception occurs.


It's used very rarely but iter accepts two arguments. If you give it two arguments, then it will call the first each iteration and stop as soon as the second argument is encountered. That seems like a perfect fit here:

with open('Jobs.txt') as openFile:
    for Stuff in iter(openFile.readline, ''):
        Jobs.append(Stuff)

I'm not sure if that's actually working like expected because openFile.readline keeps trailing newline characters (\n) so if you want to stop at the first empty line you need for Stuff in iter(openFile.readline, '\n'). (Could also be a windows thingy on my computer, ignore this if you don't have problems!)


This can also be done in two lines, without creating the Jobs before you start the loop:

with open('Jobs.txt') as openFile:
    # you could also use "tuple" instead of "list" here.
    Jobs = list(iter(openFile.readline, ''))  

Besides iter with two arguments you could also use itertools.takewhile:

import itertools
with open('Jobs.txt') as openFile:
    Jobs = list(itertools.takewhile(lambda x: x != '', openFile))

The lambda is a bit slow, if you need it faster you could also use ''.__ne__ or bool (the latter one works because an empty string is considered False):

import itertools
with open('Jobs.txt') as openFile:
    Jobs = list(itertools.takewhile(''.__ne__, openFile))
Talya answered 4/2, 2017 at 5:47 Comment(0)
P
0

The Jobs object you created is a tuple, which is immutable. Therefore, you cannot "append" anything to it.

Try

Jobs = []

instead, in which you create a list object.

Pantomimist answered 4/2, 2017 at 6:2 Comment(0)
D
0

I had also experienced the same problem. But I found the solution. For me this worked. Problem:

w=[]
x=[],
y=[],
z=[]
for i in range(4):
 w.append(i) # doesn't throw error
print(w)

This did not give error for w because I had initialized w to w = [] without comma(,) and it treated as list but when I applied the same for x it gave the error because I have initialized it as x = [], with comma here and it treated as tuple.

for i in range(4):
 y.append(i) # throws error AttributeError: 'tuple' object has no attribute 'append' 
print(y)

This solved for me and I have tried this in python3 in pycharm.

Devastating answered 30/7, 2020 at 8:55 Comment(0)
U
0

The curved brackets are not the correct bracket for creating lists. Use the square brackets for lists.

Unclog answered 7/10, 2021 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.