【发布时间】:2012-05-09 14:50:19
【问题描述】:
刚刚学习python并尝试编写一个允许用户更改文本行的脚本。出于某种原因,当提示用户输入要替换的行时出现此错误:
Traceback(最近一次调用最后一次): 文件“textedit.py”,第 10 行,在 f.write(line1) AttributeError: 'str' 对象没有属性 'write'
以及脚本本身:
f = raw_input("type filename: ")
def print_text(f):
print f.read()
current_file = open(f)
print_text(current_file)
commands = raw_input("type 'e' to edit text or RETURN to close the file")
if commands == 'e':
line1 = raw_input("line 1: ")
f.write(line1)
else:
print "closing file"
current_file.close()
【问题讨论】: