Python Read and Write File

Introduction

Files are used to store data. It is used by many programming languages to store or use its content. Python also provides various functions of python handling files. These functions are used for reading, writing, and manipulation the information of the file.

Here in the article, various code examples are provided for file handling in python such as open a file in python, load a file into python, read a text file in python, create a text file in python or write to file in python, etc.

Getting Started

If we want to read a file or to write into a file. It is necessary to open it first. To open a file in python, the python open() function is used. With open python function, python creates an object of the file which will be then always used to access a file to perform any operation on a file or to call all methods associated with it.

Syntax of Open a file in Python

 _fileobject=open(“filename”,”access_mode”,”buffering”)#python open file  
Where

  1. FileName:- Used to specify the valid file name, this is a string value.
  2. Access Mode:- Access mode determines the mode in which file is to be opened for processing i.e read, write or append, etc. it is an optional parameter, the default value is read.
  3. Buffering:- An optional and integer value. If it is 0, then no buffering takes place. if it is 1, line buffering is performed. If more than one is specified then the indicated buffer size is allotted. The system default buffer size is given if the value is negative

The above codes help to open a file in python. The file should be closed after the use of the file is completed. The python close() method used to close a file in python and cleans any unwritten information. Python automatically closes a file when the reference object of a file is reassigned to another file. But it is good practice to use the python close() method.

Syntax of Close a file in Python

 _fileobject.Close()#python close file  

Read File in Python

The following code examples read a file in python, the python read() function is used to read the content of the file which is opened. The contents can be text data, numbers, or binary data.

 # Program to show in python read text file  
 _fileobject=open("D:\aricle.txt",'r')#load file into python  
 str=_fileobject.read();#python read text file  
 print("\nThe contents of aricle.txt are :\n",str)#python printing to file  
 _fileobject.Close() #close file  

Python Read Text File

In the above example file ----- is read using the python read() functions.The python read() function returns the read contents and stores them in str, which can be printed later.

Write to File in Python

The following code examples write to file in python, the write() method is used to write the contents to the opened file. The contents can be numbers, text, and binary data. The write functions used to create a text file in python.

 # Program to show in python write to text file  
 _fileobject=open("D:\aricle.txt",'r')#open file w python  
 _fileobject.write("This is new file");#write in python
 print("\nThe contents of aricle.txt are :\n",str)#python printing to file  
 _fileobject.Close() #close file  

Python Write to Text File

The above example codes create a text file in python and write data into the file. The article.txt file is created and opened in a write mode and the write function writes the content.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال