Saturday, 1 December 2018

In this program, we will learn how to create a file & how to write something in the file by taking the input from the user in python. User will enter some data and if user wants to save the file then will press 'yes' or if not then press 'no'.

Python File Handing : File handling is an important part of any application. Python has several functions for reading, creating, deleting, and updating files. In this program ,we are using open() file handing function. Add a parameter to the open() function:

"a" - Append - Append to the end of the file.

syntax : file = open(file_name, 'a')

"w" - Write - Overwrite any existing content.

syntax : file = open(file_name, 'w')

"x" - Create - Create a file, returns an error if the file does not exist.

syntax : file = open(file_name, 'x')

a, w and x are the methods of open() function.

If you are writing using 'w' method ,the old content will be overwritten by the new content. If you don't want to overwrite the old content then use 'a' method (Append).