Wednesday, November 11, 2015

Basic File Handling -- Wring data to file and reading data from file..

In this post we will see how to open a file in write 'w' mode
a. Write some data to that file and then we will close it.
b. open the same file in read 'r' mode
c. Read the content and then we will print it and then we will close it ..

#For opening the file and wring the data to file.txt filentry= open ("file.txt",'w')
filentry.write("This is to test file operations")
filentry.close()
#For opening the file and read the data to file.txt fileread= open ("file.txt",'r')
print fileread.read()
fileread.close()
Output for this will be like this :
This is to test file operations

Note:
This program will create a file in the location given if the file doesn't exist.
If the file exist the content will be over written every time. To avoid this we can use append option 'a'

We will see different modes of file operations in our next post..


if you find any thing difficulty in understanding feel free to post your comments. I will be glad to assist you with these posts what ever posted in this blog... Thanks for your support.

No comments:

Post a Comment