PYTHON FILES I/O

In this tutorial , we will learn   how  to open a files , read it, write it, close it  & other file operations.

files

Python Files

file :  data stored on a machine.

There are two types of files in Python.

  1. Text Files
  2. Binary Files

Text files in Python

files that can easily read  by every person .

it can store in (.txt) format or  (.rtf ) format.

Binary files in Python

In binary files data is displayed in  0 and 1 format.  they are stored in (.bin) format.

file operation can be break in three steps:

  1. Opening a file
  2. Perform different file operations (Read/Write)
  3. Closing a file
files, opening closing

reference :http://http://geospatialtraining.com

Opening a Python file

to open a file we have open() function. it creates a file object.

Syntax:

here,

  • file name = file we want to open.
  • access mode= mode in which we want to open a file .

Access modes 

  • r : Opens a file for reading mode only.
  • w : Opens a file for writing mode only. Creates a new file for writing it the file doesn’t exist.
  • rb : Opens a binary file for reading mode only.
  • wb : Opens a binary file for writing mode only. Creates a new file for writing it the file doesn’t exist.
  • r+ : Opens a file for reading and writing mode.
  • rb+ :  Opens a binary file for reading and writing mode.
  • w+ : Opens a file for writing and reading mode. Creates a new file for writing it the file doesn’t exist.
  • wb+ : Opens a binary file for writing and reading mode. Creates a new file for writing it the file doesn’t exist .
  • a : Opens a file for appending. Creates a new file for writing it the file doesn’t exist.
  • a+ : Opens a file for appending and reading. Creates a new file for reading and writing it the file doesn’t exist.
  • ab : Opens a binary file for appending. Creates a new file for writing it the file doesn’t exist.
  • ab+ :  Opens a binary file for reading and writing mode. Creates a new file for reading and writing it the file doesn’t exist.

Closing a File in Python

it is done by three methods :

  • close () function :

  • try/ finally :

  • with statement :

Reading and writing files in Python

  • The read( ) function:

he read( ) function is used to read the content of a file after the file is opened in reading mode (mode=r).

Syntax

Where,

  • file = file object
  • size = number of characters we want to read. If size is not specified, it will read entire content.

Example

>>> f = open(‘example.txt’,’r’)

>>> f.read(7) #reading 7 characters from example.txt ‘This is ‘

Now the interpreter has read 7 characters of the file and if we again use read() function,  it will start reading from 8th character.

>>> f.read(7) #reading next 7 characters ‘ a text’

  • The readline( ) function :

The readline( ) function is used to read the content line by line. It is useful for the large files and we can get access to any line at any moment.

Example

Let’s create a file 

test.txt

 containing multiple lines as:

Now let’s see how 

readline( )

 function is used in 

test.txt

.

>>> x = open(‘test.txt’,’r’)

>>> x.readline() #to display first line This is line1.

>>> x.readline(2) #to read second line This is line2.

>>> x.readlines() #to read all lines [‘This is line1.’,’This is line2.’,’This is line3.’]

  • The write( ) function :

The write( ) function is used to write into files in Python opened in writing mode.

Opening a file in writing mode will create a new file if the file we attempt to open doesn’t exist.

Syntax

Example

Let’s imagine we don’t have file xyz.txt . therefore it will be created when we will  open the file in writing mode.

>>> f = open(‘xyz.txt’,’w’) #opening in writing mode

>>> f.write(‘Hello \n World’) #writing Hello World to the file Hello World

>>> f.close() #closing the file

Renaming files in Python

The rename( ) function

The function 

rename( )

 is used to rename a file in Python. For this we need to import os module first.

Here is the syntax.

Where,

  • src = file we want to rename
  • dest = new name for the file src

Example

>>> import os

>>> #renaming xyz.txt to abc.txt

>>> os.rename(“xyz.txt”,”abc.txt”)

Current position in python files

In Python, it is possible to know current position in a file using a function called 

tell( )

. Similarly, we can change the current position by using the function 

seek( )

.

Example

>>> f = open(‘example.txt’) #example.txt we created earlier

>>> f.read(4) #let’s get to the 4th position first This

>>> f.tell() #returns the current position 4

>>> f.seek(0,0) #repositioning the position to 0 again

This is how we can get the current position in the file and shift the position as well.

http://www.javatpoint.com

 

%d bloggers like this: