w3resource logo


Python Exercises

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

Python Code Editor:

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

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.