Есть код:
f = open('text.txt', 'w')
f.write("qwerty / asdfgh / zxcvbn")
f = f.replace('/', '\n')
f = open('text.txt', 'r')
f = f.read()
print(f)
Почему метод replace не работает? выдает ошибку:
'int' object has no attribute 'replace.
Stack Overflow на русском — это сайт вопросов и ответов для программистов. Присоединяйтесь! Регистрация займёт не больше минуты.
Присоединиться к сообществуМетод replace относится к строками, а не файловым объектам:
with open('text.txt', 'w') as f:
text = "qwerty / asdfgh / zxcvbn"
text = text.replace('/', '\n')
f.write(text) # 'qwerty \n asdfgh \n zxcvbn'
with open('text.txt', 'r') as f:
text = f.read() # 'qwerty \n asdfgh \n zxcvbn'
print(repr(text))
print(text)
def open file(file name. mode): the_file = open(file_name. mode. encoding='utf-8') return the_file def next_line(the_file): line = the_file.readline() line = line.replace("/", "\n") return line
f.write("qwerty / asdfgh / zxcvbn".replace('/', '\n'))
'_io.TextIOWrapper' object has no attribute 'replace'