w3resource

Python File I/O: Append text to a file and display the text

Python File I/O: Exercise-3 with Solution

Write a Python program to append text to a file and display the text.

Sample Solution:-

Python Code:

def file_read(fname):
        from itertools import islice
        with open(fname, "w") as myfile:
                myfile.write("Python Exercises\n")
                myfile.write("Java Exercises")
        txt = open(fname)
        print(txt.read())
file_read('abc.txt')

Sample Output:

Python Exercises                                                                                              
Java Exercises 

Flowchart:

Flowchart: File I/O:  Append text to a file and display the text.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to read first n lines of a file.
Next: Write a Python program to read last n lines of a file.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.