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

前端 未结 3 1539
心在旅途
心在旅途 2021-01-20 02:20

Can anyone help me with this code?

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


        
3条回答
  •  Happy的楠姐
    2021-01-20 03:06

    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.

提交回复
热议问题