MarshallCodeX
MarshallCodeX

Reputation: 43

AttributeError: '_io.TextIOWrapper' object has no attribute 'append'?

print to ask user for their name

name = input("what is your name ")

file_name = str(input("What do you want to name this .txt file\n> "))
if file_name[-4:] != ".txt":
    file_name += ".txt"

greet them

asking for there name and employee names

print("Why hello",name,"now lets caculate that employee's next pay check")
def employees():
    emplist = []
    while True:
        names = input('What is the name of the employee')
        if names == 'done':
            break
        else:
            emplist += [names]
            print(emplist)
    pay(emplist)

ask for hourly pay

    def pay(emplist):


for person in emplist:
        print("now i need hourly pay of",person,)
        pay = float(input("> "))

ask for the hours they worked

print("now i need the hours worked by",person,)
    hours = float(input("> "))

calculate the pay

doing math

if hours > 40:
        over = 1.50
        overtimeR = over * pay
        overtime = overtimeR * (hours-40)
        hours += 40
    else:
        overtime = 0

make them different responses

not complete yet

if overtime > 0:
        hours2 = 40
        totalpay = (pay * hours2) + overtime
        pay_without_overtime = pay * hours2

else:
    totalpay = (pay * hours) + overtime

person_2 = ""

person_2 += person

info = ("Employee: "+str(person_2)+"\nTotal Hours: "+str(hours))




with open(file_name, 'a+')as file_data_2:
    file_data_2.append(info)





employees()

But IT gives me error

how do i fix this

AttributeError: '_io.TextIOWrapper' object has no attribute 'append'

Upvotes: 1

Views: 4410

Answers (1)

arudzinska
arudzinska

Reputation: 3331

When you open a file with with open(file_name, 'a+') as file_data_2: the variable file_data_2 becomes an instance of a class _io.TextIOWrapper which indeed does not have such attribute. If you want to see what attributes/methods are available for any variable that you create you can easily do that in the interactive mode of Python. Open terminal, run your Python (Python 3 in my case):

$ python3

First, open your file and store it in a variable similarly to what you did in your code:

>>> file = open("sample.txt", 'a+')

The variable file is now an instance of the _io.TextIOWrapper class. You can check the available methods for the class with the command:

>>> dir(file)

And this is the output:

['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', 
'__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', 
'__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', 
'__init__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', 
'__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__sizeof__', '__str__', '__subclasshook__', '_checkClosed', 
'_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 
'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 
'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 
'read', 'readable', 'readline', 'readlines', 'seek', 'seekable', 
'tell', 'truncate', 'writable', 'write', 'writelines']

As you see, there is no 'append' method. However, there is 'write' and I suppose that is what you need.

Upvotes: 1

Related Questions