1

I have a hello.py file which asks user for their name and prints them a welcome message.

import subprocess

filename = "hello-name.txt"

fout = open("out.txt", "w")

with open(filename, "r+") as f:
    lines = f.readlines()

your_name = input("What is your name? ")
title_name = your_name.title()

for line in lines:
    line = fout.write(line.replace("[Name]", your_name))
    line = fout.write(line.replace("[Title]", title_name))
    print(line.strip())

    subprocess.call(["notepad.exe", "out.txt"])

This is the content of my hello-name.txt file

Hello [Name]
Welcome to the world [Title]

My Question

When run hello.py it asks user for their Name and as soon as the name is entered, Python gives the following error :

line = fout.write(line.replace("[Title]", title_name))
AttributeError: 'int' object has no attribute 'replace'

Kindly help me to fix this issue.

Thank You

3 Answers 3

2

The write method returns the number of characters written, not the value written. Don't assign the result to line if you want to keep using it. If the goal is to perform both replacements before writing, do them both, then write, e.g.:

for line in lines:
    line = line.replace("[Name]", your_name)
    line = line.replace("[Title]", title_name)
    fout.write(line)
Sign up to request clarification or add additional context in comments.

8 Comments

Thank You. I don't get any error but now why "out.txt" is empty instead of containing the message?
You failed to flush or close the file, so your data is stuck in user-mode buffers. Either close it, or if you need to keep it open for future appends, call fout.flush() so the user mode buffers are sent to the kernel to actually write the data to disk. This is the general rule with any file that you pass to some other API (or process) by name; user-mode buffered data is only visible on the handle it was written to until it is flushed or closed.
I closed it by with "fout.close()". Just one more question. The "out.txt" file now contains just one message. It was not able to load the message of second line "Welcome to the world [Title]".
Sounds like you shouldn't be invoking notepad (or closing the file) until the loop is complete. Dedent the subprocess invocation.
Thank You for the help
|
1

There are two problems with this script:

  1. As ShadowRanger pointed out, you're assigning the result from write to line; this overwrites the content.
  2. You don't close the output file before opening it in Notepad.

An easy way to make sure a file is closed is to open it in a context, using the with keyword. You already did this when you read the input file; just do the same thing for the output file, and open Notepad after the with block (i.e. after you've written both lines and closed the file):

import subprocess

with open("hello-name.txt", "r+") as f:
    lines = f.readlines()

your_name = input("What is your name? ")
title_name = your_name.title()

with open("out.txt", "w") as fout:
    for line in lines:
        line = line.replace("[Name]", your_name)
        line = line.replace("[Title]", title_name)
        fout.write(line)
        print(line.strip())

subprocess.call(["notepad.exe", "out.txt"])

Comments

0

The return from fout.write is an integer. Then you are trying to replace on an integer (it isn't giving you the replaced line.

Try:

import subprocess

filename = "hello-name.txt"

fout = open("out.txt", "w")

with open(filename, "r+") as f:
    lines = f.readlines()

your_name = input("What is your name? ")
title_name = your_name.title()

for line in lines:
    line = line.replace("[Name]", your_name)
    line = line.replace("[Title]", title_name)
    fout.write(line)
    print(line.strip())

fout.close()
subprocess.call(["notepad.exe", "out.txt"])

I changed it to a single fout since my guess is you don't want the double printing (but you can change it back). You probably also want to make the notepad call after parsing everything (and closing the file as others have mentioned)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.