16 file handling in python

 1. Using file handling we can do file operations


# How to open a file in python ?

1. We can open a file two ways in python 

    1. open()

    2. With open()


# OPEN

1. Using open we can open only one file at a time and we have to close a file explicity


# WITH OPEN 

1. Using with open we can open multiple files at a time and no need to close files explicity


# File Open Modes In Python


r    ==>     It opens a file for read only.


w    ==>     It allows write-level access to a file. If the file already exists, then it’ll get overwritten. It’ll create a new file if the same doesn’t exist.


a    ==>     It opens the file in append mode.


a+   ==>     It opens a file FOR append AND read


rb   ==>     It opens a binary file for read


1)  Read file

fh = open('names.txt', 'r')


## To open a file from another path

#fw = open(r'C:\Users\smellamput001\Desktop\BACKUP_EPRO\RRR.txt', 'r')


## Using read we can read entire file data and return in string format

fdata = fh.read()

print(fdata)


## To read the first five characters

#fdata = fh.read(5)

#print(fdata)


## Using readline we can read only one line and return in string format

#line  = fh.readline()

#print(' 1 ', line)


#line  = fh.readline()

#print(' 2 ', line)


## Using readlines we can read total lines in a file and  return in list format

lines  = fh.readlines()

print(' READ LINES ', lines)


# To close a file

fh.close()

2) Write into file
'''
1. To do write operation on file we have to open a file with 'w' mode
2. If you open a file with write mode if file is not exist it will crate a new file, 
if file is exist it will remove old data and write new data.

'''

fw = open('RRRR.txt', 'w')


# To write data into file
fw.write('\nHi Good Morning\n')
fw.write('How are you')

# To close a file
fw.close()


3) Append the file
'''
1. To do append operation on file we have to open a file with 'a' mode
2. If you open a file with append mode 
if file is not exist it will crate file, 
if file is exist it will add new data after old data.

'''

fw = open('append_names.txt', 'a')

# To write data into file
fw.write('\nHi umamahesh\n')
fw.write('How are you\n ')

# To close a file
fw.close()


4) open the file using withopen() function

# reading names.txt and writing into names_write.txt

with open('names.txt') as fr, open('names_write.txt', 'w') as fw:
    fw.write(fr.read())

5 ) tell( function

'''

1. Tell is used to find the file handler position

'''


fh = open('names.txt')


# To know File handler position before read any data

print(' Before read :', fh.tell())


# Using readline we can read only one line and return in string format

line  = fh.readline()


# To know File handler position after read a line

print(' After read :', fh.tell())


# To close a file

fh.close()

5) Seek()
'''
1. Seek is used to bring the file handler position 
where ever we required

'''

fh = open('names.txt', 'r')

## reading entire data from file
line  = fh.read()

## If you wnat to bring file handler position to starting
print(' FH position Before seek :', fh.tell())
fh.seek(8)
print(' FH position After seek  :', fh.tell())
print(' 1 : ',fh.readline())

## If you wnat to bring file handler to 8th position
#print(' FH position Before seek :', fh.tell())
#fh.seek(8)
#print(' FH position After seek  :', fh.tell())
#print(fh.readline())

# To close a file
fh.close()




Comments

Popular posts from this blog

1 PYTHON PROGRAMMING

4 Tuple data types