Wednesday, 25 January 2017

FILE HANDLING IN PYTHON

Programming languages programs memory allocation takes place in RAM area at the time of execution of the program. RAM is a volatile so that after execution of the program the memory is going to be deallocated. Programming languages programs are good at processing the data but they cannot store the data in permanent manner. After processing the data we have to store the data in permanent manner. If the data is stored in permanent manner then we can use the data whenever we want. We can store the data in permanent manner by using files.

File is a named location on disk, which is used to store the related data in permanent manner in the hard disk. By using python program we can put the data into the file, we can get the data from the file and we can modify the data of the file. Before going to read or write or modify the data of the file, we have to open the file. We can open the file by calling one predefined function called open. At the time of opening the file we have to specify the file modes. Mode of the file indicates for what purpose we are going to open the file.

Python supports following file modes –
r        open a file for reading(default).
w      open a file for writing, creates a new file if it does not exist or truncates
          the file if it exists.
x       open a file for exclusive creation, if the file already exists, the operation
          fails
a       open for appending at the end of the file without truncating it. Creates
          a new file if it doesnot exist.
t        open in textmode(default)
b       open in library mode
+       open a file for updating

Syntax – x=open(“filename”,”mode of the file”)

After executing the open function it will create file object with the specified modes. By calling the methods on the file object we can read the data from the file, we can write the data into the file and update the data of the file. After performing the operations on the file object we have to close that file object too. We can close the file object by calling close method

Example –
x=None
try:
    x=open("abc.txt")
    print 'file is opened'
    print x
    data=x.read()
    print data
except:
    print 'error occured'
finally:
    if x:
        x.close()
        print 'file is closed'

Output –
file is opened
<open file 'abc.txt', mode 'r' at 0x014CD1D8>
rakesh kumar prasad
bamunimaidam, guwahati-21
assam
+918011807550
file is closed

Example –
x=None
try:
    x=open("abc.txt")
    print 'file is opened'
    print x
    data=x.read(15)
    print data
    line=x.readline()
    print line
    lines=x.readlines()
    print lines
except:
    print 'error occured'
finally:
    if x:
        x.close()
        print 'file is closed'

Output –
file is opened
<open file 'abc.txt', mode 'r' at 0x01D5D1D8>
rakesh kumar pr
asad

['bamunimaidam, guwahati-21\n', 'assam\n', '+918011807550']
file is closed


Whenever we open the file by default file pointer points the zero location. By using tell function we can know the current file pointer location. By using seek() function we can change the file pointer from one location to another location.

Example –
x=open("abc.txt")
print x.tell()
data = x.read(5)
print data
print x.tell()
data1 = x.readline()
print data1
print x.tell()
x.seek(0)
print x.tell()
data2 = x.readline()
print data2
print x.tell()
x.seek(0)
print x.tell()
data3 = x.read()
print data3
print x.tell()

Output –
0
rakes
5
h kumar prasad

21
0
rakesh kumar prasad

21
0
rakesh kumar prasad
bamunimaidam, guwahati-21
assam
+918011807550
68


PROGRAM TO WRITE DATA INTO THE FILE
x=None
try :
    x=open("xyz.txt","w")
    x.writelines('Python supports OOps \n Hii Python')
    print 'data stored in xyz.txt'
except :
    print 'error occured while writing data into the file'
finally:
    if x:
        x.close()
        print 'end'

Output –
data stored in xyz.txt

end

No comments:

Post a Comment