【问题标题】:python write file syntax errorpython写入文件语法错误
【发布时间】: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()

【问题讨论】:

    标签: python string syntax


    【解决方案1】:

    改变这个:

    f.write(line1)
    

    进入这个:

    current_file.write(line1)
    

    发生错误,因为您正在访问f,就像它是一个文件一样,但它只是用户给出的文件名。打开的文件存储在current_file变量中。

    此外,您正在以读取模式打开文件。看看open()'s documentation

    open(name[, mode[, buffering]])

    mode 最常用的值是 'r' 用于读取'w' 用于写入(如果文件已存在则截断文件)和'a' 用于追加(其中在某些 Unix 系统上意味着所有写入都附加到文件的末尾,而不管当前的查找位置如何)。 如果省略模式,则默认为'r'

    【讨论】:

      【解决方案2】:

      你应该这样做:

      current_file.write(line1)
      

      发生了什么?您将 文件名 存储在 f 中,然后用它打开了一个文件对象,该文件对象存储在 current_file 中。

      错误消息试图准确地告诉您:f 是一个字符串。字符串没有write 方法。在第 10 行,您尝试对存储在变量 f 中的对象调用方法 write。它不能工作。

      学习编程将涉及学习阅读错误消息。不用担心。随着您的进行,它会变得更容易。

      【讨论】:

        【解决方案3】:

        改变这个:

        current_file = open(f)
        

        到这个:

        current_file = open(f, 'w')
        

        还有这个:

        f.write(line1)
        

        到:

        current_file.write(line1)
        

        【讨论】:

        • +1 用于建议'w' 模式。也许在您添加第二个建议之前投反对票?
        【解决方案4】:

        您正在尝试在f 上使用write,这是一个字符串(包含raw_input() 结果),而您可能应该在文件上写入。此外,它被认为更符合 Python 风格,是使用with 语句打开文件的更好做法,因此您可以确保文件在任何可能的情况下都会关闭(包括意外错误!):

        python 3.x:

        def print_from_file(fname):
            with open(fname, 'r') as f:
                print(f.read())
        
        f = input("type filename: ")
        with open(f, 'w') as current_file:
            print_from_file(f)
            commands = input("type 'e' to edit text or RETURN to close the file")
            if commands == 'e':
                line1 = input("line 1: ")
                current_file.write(line1)
            else:
                print("closing file")
        

        python 2.x:

        def print_from_file(fname):
            with open(fname, 'r') as f:
                print f.read()
        
        f = raw_input("type filename: ")
        with open(f, 'w') as current_file:
            print_from_file(f)
            commands = raw_input("type 'e' to edit text or RETURN to close the file")
            if commands == 'e':
                line1 = raw_input("line 1: ")
                current_file.write(line1)
            else:
                print "closing file"
        

        【讨论】:

          猜你喜欢
          • 2014-01-25
          • 2020-08-09
          • 1970-01-01
          • 2023-01-13
          • 1970-01-01
          • 2012-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多