x

Python Files

PREV    NEXT

Till now we are used variables to store the data,but the data which ever stored in variables it will not available once the application end.To achieve this we are going to use the file concept.


In python we can handling the file based on three components.They are

1.Open File
2.Process the file (wirite and read operations)
3.Close file

Types of Files:

As we known two types of files are available.They are

            1. Text Files
2. Binary Files

1. Text File:

A text file is nothing but which is containing alphabates,numbers,symbols,no specific formatings it is called Text file

2. Binary File:

  • A Binary file is nothing but a file is stored in binary format which is not readable by human beings except machines this are called binary file
  • In This tutorial we can work both files but before that we are going to discuss about it ,it should what are the mode have to perform operation on the files. 
Mode Explanation
‘w’

 

It will opens the file for writing the data,if file is not found then it will create new file and then it will write

 

‘r’

 

It will open the file for reading ,if file not found it will returns FileNotFoundError error.

 

‘a’

 

It will opens the file for append mode ,if file is not found then it will create new file and then it will write,if found it append the data into that file

 

‘w+’

 

It will opens the file for writing and reading the data,if file is not found then it will create new file and then it will write

 

‘r+’

 

It will open the file for writing and reading ,if file not found it will returns FileNotFoundError error.

 

‘a+’

 

It will opens the file for reading and append mode ,if file is not found then it will create new file and then it will write,if found it append the data into that file

 

‘wb’

 

It will opens the file for writing the data in binary format,if file is not found then it will create new file and then it will write

 

‘rb’

 

It will open the file for reading binary ,if file not found it will returns FileNotFoundError error.

 

‘ab’

 

It will opens the file for append mode binary format ,if file is not found then it will create new file and then it will write,if found it append the data into that file

 

‘wb+’

 

It will opens the file for writing and reading the data in binary format,if file is not found then it will create new file and then it will write

 

‘rb+’

 

It will open the file for writing and reading in binary format ,if file not found it will returns FileNotFoundError error.

 

‘ab+’

 

It will opens the file for read and append mode binary format ,if file is not found then it will create new file and then it will write,if found it append the data into that file

 

Methods for file operations:

Method

 

Explanation
read(num_arg)

 

It will read the number of characters which number passed as an argument ,it will return as a string

 

write(str_arg)

 

It can write the string argument to the file and then it will return no.of character to the file

 

readline()

 

read a single line ,returns it as a string

 

readlines()

 

read the content line by line read them as collection strings

 

close()

 

close the file

 

tell()


 

returns the current position of file pointer

 

seek(offset,origin)

 

Moves the file pointer to given offset from the origin

 

1. Open A File:

 To perform read or write etc any kind of operation we need to open the file by using the in built fucntion open.This will retrun a file object,by using this object we can perform the required operation

Syntax:

            object=open(file_name,mode,buffering)

file_name:
It is a file name representing in string format

mode:
This is the only actual parameter to open the file in which i.e read,write etc.By default file will be open in read mode

buffering:
The default value is “0”,means buffering will not take place.If the value is “1” then buffering will take place .If more than “1” buffering will take with respect to buffer size.If negative ,then it will consider default operation.

Example 1:

Script for understanding how to open file and known the attributes of that file

Output:

Example 2:

How to read entire data in file

Output:

Example 3:

How to close a file

Output:

Using with exception and close file

try:

Output:

Example 4:

How to write into file:

Output:

Example5:

How to read a file:

Output:

Example 6:

How rad the offset and cheange the offset 

Output:

Example 7:

How to rename and delete the file

Output:

Delete the file:

import os

os.remove( “new_file.txt” )

Output:

Example 8:

File operations by suing with clause

Output:

Example 9:

File with exception handling

Iteration1:

Input1:

Output:

Iteration2:

Input1:

give the existing file name ex:file.txt

Enter file name: file1.txt

Output:

PREV    NEXT



Like it? Please Spread the word!