You want f.write, not outfile.write...
outfile is the name of the file as a string. f is the file object.
As noted in the comments, file.write expects a string, not a sequence. If you wanted to write data from a sequence, you could use file.writelines. e.g. f.writelines(self._headers). But beware, this doesn't append a newline to each line. You need to do that yourself. :)
You want f.write, not outfile.write...
outfile is the name of the file as a string. f is the file object.
As noted in the comments, file.write expects a string, not a sequence. If you wanted to write data from a sequence, you could use file.writelines. e.g. f.writelines(self._headers). But beware, this doesn't append a newline to each line. You need to do that yourself. :)
Assuming that you want 1 header per line, try this:
with open(outfile, 'w') as f:
f.write('\n'.join(self._headers))
print done
AttributeError: 'str' object has no attribute 'write'
How do i fix this : AttributeError: 'str' object has no attribute 'current'
stream.write(msg + self.terminator) AttributeError: 'str' object has no attribute 'write'
AttributeError: 'str' object has no attribute 'write' - Post.Byes
Videos
There is something wrong with this function. It shows no errors, but when I try to run it, It says AttributeError: 'str' object has no attribute 'current'. (BTW, i am trying to run it as a flet on spyder)
def leapyears(e):
days_in_month = {1: 31, 3: 31, 4: 30, 5:31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31 }
month = int(EnterMonth_text.value)
year = int(EnterYear_text.value)
if year % 100 == 0:
if year % 400 == 0:
leap_year = True
elif year % 4 == 0:
leap_year = True
else:
leap_year = False
if month == 2 :
if leap_year:
days_in_month[2] = 29
else:
days_in_month[2]= 28
output_textfield.value= days_in_month[month]
page.update()
I have some experience with programming in Java, C++, etc. and I am trying to write a simple "To-Do List" program to get used to Python. I'm running into the error: str object has no attribute "completed" when trying to iterate over the list of tasks, check their completion status, and display them.
Here are some relevant pieces of the program:
Constructor for the Task class
def __init__(self, task_name):
self.task_name = task_name
self.completed = False
In the ToDoList class (which holds a list of the task instances created by the user) this is the iteration throwing the error in question:
for idx, task in enumerate(self.tasks, start=1):
status = "Completed" if task.completed else "Incomplete"
print(f"{idx}. {task.task_name} - {status}")
I thought, potentially the problem lies in the fact that the enumerate function is grabbing the string value of the task instance, rather than the object itself, so maybe I can iterate over it the old fashioned way and get around it. So I tried it like this:
counter = 1
for task in self.tasks:
status = "Completed" if task.completed else "Incomplete"
print(f"{counter}. {task.task_name} - {status}")
counter += 1
Yet, it throws the same error. I know there is something I am missing or not understanding correctly here. What is it?
Thanks!
You should be using itinerary_file.write and itinerary_file.close, not file_name.write and file_name.close.
Also, open(file_name, "a") and not open('file_name', "a"), unless you're trying to open a file named file_name instead of itinerary.txt.
An attribute error means that the object your trying to interact with, does not have the item inside it you're calling.
For instance
>>> a = 1
>>> a.append(2)
a is not a list, it does not have an append function, so trying to do this will cause an AttributError exception
when opening a file, best practice is usually to use the with context, which does some behind the scenes magic to make sure your file handle closes. The code is much neater, and makes things a bit easier to read.
def save_itinerary(destination, length_of_stay, cost):
# Itinerary File Name
file_name = "itinerary.txt"
# Create a new file
with open('file_name', "a") as fout:
# Write trip information
fout.write("Trip Itinerary")
fout.write("--------------")
fout.write("Destination: " + destination)
fout.write("Length of stay: " + length_of_stay)
fout.write("Cost: $" + format(cost, ",.2f"))
The issue is here. You are using same name for both the file object and loop variable. When you call f.write(l + "=" + f + "\n"), f is no longer a file object(Because the the loop variable overwritten f into a string object)
with open("./flash_cards.txt", "w+") as f:
for l, f in flash_cards.items():
f.write(l + "=" + f + "\n")
you have f in your for inside the with
You may choose another variable, and the problem will be removed. As example:
with open("./flash_cards.txt", "w+") as f:
for l, value in flash_cards.items():
f.write(l + "=" + value + "\n")