File Handling in Python

File may be opend in following modes in python.

Read(r): It opens the file in read mode if file exists gives error if file does not exist.
Append(a): It open the file in append mode, if file not exists it creates the file.
Write(w): It opens the file in write mode , if file not exists it create the file.

Program to display the content of the given file:

f1 = open("myFile.txt", "r")
print(f1.read())

Program to Write the content into the given file.

f = open("File2.py", "w")
f.write("New Content Added to File")

If File2.py not exist its being created and given content is written in the file.

f = open("File2.py", "a")
f.write("content appended to the file")

In this example the given content is appended to the file.

Click Here

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial