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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/file/python-io-exercise-3.php