File Funciton

M3-R5.1 · Chapter 7: File Processing · 4 min read

File Functions: There are following file function in python

Function

Description

open()

Used to open a file.

close()

Used to close the file from the program.

read()

Used to read whole content at a time.

readline()

Used to read a single line from the file.

readlines()

Used to read all the content in the form of lines.

write()

Used to write a string to the file.

writelines()

Used to write multiple strings at a time.

Append()

This function append string to the file instead of overwriting the file.

Tell()

Returns the current file position

Seek()

Change the file position

rename()

Rename the function module but must import the “os” module

Remove()

Use to delete the file by supplying the file name but must import the “os” module

Reading Data From a File: There are following function to read the data from a file

                        i)  Read()                    ii) Readline()  iii)       readlines()

Read() Function: 

      • Read at most n bytes : if no n is specified , reads the entire file.
      • Returns the read bytes in the form of a string.

  Syntax:     <filehandle>.read([n])  # here n is optional

f=open("jai.txt",'r')

data=f.read()

print(data)

f.close()

Read frist 10 cahracter forma file

f=open("jai.txt",'r')

data=f.read(10)

print(data)

f.close()

 readline() Function:  This function enables us to read the file line by line by using this .it reads one line from the file and place the file pointer to the beginning of the new line. If we use the redline() method twice then we get the first 2 line of the file.

f=open("jai.txt")

data=f.readline()

data1=f.readline()

print(data)

print(data1)

f.close()

f=open("jai.txt")

print(f.readline())

print(f.readline())

print(f.readline())

f.close()

Syntax:     <filehandle>.readine()

               

readlines() Function: 

  • Reads all the lines of a text file until end of the file
  • Returns in the form of lists objects

Syntax:     <filehandle>.readines()

readlines() function

f=open("jai.txt")

data=f.readlines()

print(data)

f.close()

Write () Function:  This function writes a fixed sequence of characters to a file. the write() does not actually write data to a file but to a buffer. When the close () function is called then it is written in file

Note: In order to write data into a file , we must open the file in write mode.

Example:

f=open("jai.txt","w")

f.write("I am writing my first sentence\n")

f.write("I am writing my Second sentence\n")

f.close()

f=open("jai.txt")

data=f.read()

print(data)

f.close()

Writelines () Function:  It write a sequence of the strings to a file.

  Syntax:     <filehandle>.writelines(seq)

Example:  colour=["red\n", "blue\n", "Green\n", "Yellow"]

lines = ["\n Hello World\n", "Welcome to Python\n", "File handling is easy!\n"]

f=open("jai.txt","w")

f.writelines(colour)

f.writelines(lines)

f.close()

f=open("jai.txt")

data=f.read()

print(data)

f.close()

append () Function:  Appending is writing data at the end of the file

Example: f=open("jai.txt","a+")

f.write("\nPink")

f.close()

f=open("jai.txt")

data=f.read()

print(data)

f.close()

# Appending list of data

colour=[“\nWhite”, “\nblack\n”, “\nIndigo”, “\nPurple”]

f=open(“jai.txt”,”a+”)

 f.writelines(colour)

 f.close()

Examples of tell() and seek() methods:

test=["\n Different ","Colours","name"]

f=open("jai.txt","a+")

f.writelines("text")

print("position of the file cursor:",f.tell())

f.seek(0)

print("position of the file cursor:",f.tell())

print(test)