alkopop79
alkopop79

Reputation: 539

python write file syntax error

just learning python and trying to write a script that allows the user to change lines in a text. For some reason I get this error when prompting the user to enter something for the line to be replaced:

Traceback (most recent call last): File "textedit.py", line 10, in f.write(line1) AttributeError: 'str' object has no attribute 'write'

and the script itself:

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()

Upvotes: 0

Views: 2187

Answers (4)

Nadir Sampaoli
Nadir Sampaoli

Reputation: 5555

You're trying to write on f which is a string (contains a raw_input() result) while you should presumably write on a file. Also, it's considered more pythonic and a better practice opening a file using a with statement, so you'll be sure that your file will get closed in any possible case (unexpected errors included!):

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"

Upvotes: 0

Thanasis Petsas
Thanasis Petsas

Reputation: 4448

change this:

current_file = open(f)

to this one:

current_file = open(f, 'w')

and this:

f.write(line1)

to:

current_file.write(line1)

Upvotes: 1

Tadeck
Tadeck

Reputation: 137350

Change this:

f.write(line1)

into this:

current_file.write(line1)

The error occured, because you were accessing f as if it was a file, but it is only a file name given by the user. The opened file is stored in the current_file variable.

Additionally you are opening the file in read mode. Take a look at open()'s documentation:

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

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'.

Upvotes: 4

Daren Thomas
Daren Thomas

Reputation: 70314

You should be doing:

current_file.write(line1)

What happened? You stored the filename in f and then opened a file object with it, which you stored in current_file.

The error message was trying to tell you exactly that: f is a string. Strings don't have a write method. On line 10 you tried to call the method write on the object stored in the variable f. It can't work.

Learning to program will involve learning to read error messages. Don't worry. It gets easier as you go along.

Upvotes: 1

Related Questions