We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250


File Handling in Python

File Handling in Python

File Handling in Python

Python provides built-in libraries for handling file functions like create, update, append, read and write.

Open ():   Returns file object by allowing file opening action with file modes

Syntax  : Open ( <file name> , < file modes >)

<file name> : Name of file

<file mode>: File mode specifies the certain actions need to be performed once file object is accessed

Example: Open file in read mode


File Modes

'r'Default file mode for reading file content
'w'Write mode to write content in file. if file does’t exist, it create new file & overrides if exists.
'x'Create a new file. If file already exists, the operation fail.
'a'Append content if file exist & create a new file if it does not exist
't'Default mode for text file
'b'Binary mode if binary file needs to be opened
'+'Opens file in read and write mode

This allows us to perform operations like reading, write, append on the file objects. Given below are a list of file modes available in the file object

Create a new file with “x” file mode

It is possible to create a new file using the open() function, the only thing required is of the addition of “x” file mode in file opening function followed by the new file name.

In below example, we are creating a new file called “Galaxy.txt” bypassing “x” in file mode


read(): It reads the content from file

Syntax :   <file object>.read()

In the below example, we have created a file object to open file “License.txt” in read “r” mode.


We have used a read () method to read the contents of the “License.txt” file. variable “str” is storing the string contents using read() methods. we have used the print method to display output on the console. In our next example, we are using a read(n) method to read a fixed number of characters from the beginning of the file. read(10)~ is reading 10 characters from a file .


readline() : It read contents by line

Syntax : < file object>.readline()

In the below example, we are using readline() method to read content by line . we have opened the “License.txt” file in read “r” mode to read its content.  In the below example, it is returning a single line from the file.


To read all content from a file , it is essential to write a loop over file object. Refer below example for clear understanding.

for x in file

write() : It write contents to the file file

syntax : <file object>.write(<“contents ”>)

In the below example, we have opened the “License.txt” file in write mode, we are writing strings in file with write() function. Write function takes string arguments/texts as an argument and writes to the file.

>>file.write("new content")
new content

To append content in an existing file, we are opening the file in append “a” mode. This will append new text to the end of line of existing file. In the below example, we are appending new text in the “News.txt” file  & we are revalidating it by reading back the contents from the “New.txt”  file.

>>file.write("This is appended text")
new content This is appended text

close() : closes the file object

Syntax: <file object>.close()

In the below example, after completing read and write operation on a file . we closed the file object with a close() method.

>>file.write("New Content")
new content

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring
Besant Technologies WhatsApp