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"
要求提供姓名和员工姓名
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)
def pay(emplist):
for person in emplist:
print("now i need hourly pay of",person,)
pay = float(input("> "))
print("now i need the hours worked by",person,)
hours = float(input("> "))
做数学
if hours > 40:
over = 1.50
overtimeR = over * pay
overtime = overtimeR * (hours-40)
hours += 40
else:
overtime = 0
尚未完成
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()
我该如何解决这个问题
AttributeError: '_io.TextIOWrapper' object has no attribute 'append'
答案 0 :(得分:0)
当您使用with open(file_name, 'a+') as file_data_2:
打开文件时,变量file_data_2
将成为类_io.TextIOWrapper的实例,该类实际上没有此类属性。如果要查看您创建的任何变量可用的属性/方法,可以在Python的交互模式中轻松完成。打开终端,运行你的Python(在我的例子中是Python 3):
$ python3
首先,打开文件并将其存储在变量中,与您在代码中的操作类似:
>>> file = open("sample.txt", 'a+')
变量file
现在是_io.TextIOWrapper
类的一个实例。您可以使用以下命令检查类的可用方法:
>>> dir(file)
这是输出:
['_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']
如您所见,没有'追加'方法。但是,有'写'我想这就是你需要的。