File Handling Python Notes

Files

  • A file is a named location on a secondary storage media where data are permanently stored for later access

Types of Files

  • Text File
  • Binary File
  • CSV File

Text File

  • A text file can be understood as a sequence of characters consisting of alphabets, numbers and other special symbols.
  • File with extensions like .txt, .py, .csv etc are text files
  • Each line of a text file is terminated by a special character, called the End of Line (EOL).
  • the default EOL character in Python is the newline (\n).
  • Contents in a text file are usually separated by whitespace, but comma (,) and tab (\t) are also commonly used to separate values in a text file.

Opening Text File

  • file_object= open(file_name, access_mode)
  • Default access mode is ‘read mode’
  • Once we are done with the read/write operations on a file, it is a good practice to close the file.
  • file_object.close()

Access mode

Image taken from NCERT text book

Writing to Text File

  • To write to a text file, it must be opened in Write or Append mode.
  • We can use following methods to write
    • Write() – for writing a single string
    • Writelines() – for writing a sequence of string
  • New data is added at the end of file object

Write() method

  • Write method takes a string as argument.
  • Number has to be stored as string
  • After execution it returns total number of characters written in file
  • It actually write data onto buffer which is transferred to file after close() is executed.

Filo = open(‘C:\san\sample.txt’,’w’)

Filo.write(“welcome”)

Filo.close()

Output

7

Writeline() method

  • Writelines() is used to write multiple strings to a file
  • We need to pass iterable object containing string to writelines()
  • Writelines() does not return any value.

Filo = open(‘C:\san\sample.txt’,’w’)

Filo.writelines([“welcome\n”,”to python\n”,”Byee\n”])

Filo.close()

Reading from text file

  • Read()
  • Readline([n])
  • Readlines()

read()

  • Reads specified no of bytes of data from data file
  • If no argument or negative number is specified, the entire file content is read

Filo = open(‘C:\san\sample.txt’,’w’)

Filo.writelines([“welcome\n”,”to python\n”,”Byee\n”])

Filo.close()

Filo = open(‘C:\san\sample.txt’,’r’)

Filo.read(5)

Filo.close()

Output

welco

readline()

  • Reads complete line from a file where each line is terminated with ‘\n’
  • It can also be used to read specified no of bytes of data from a file.
  • If no argument is specified, it returns first line.

Filo = open(‘C:\san\sample.txt’,’w’)

Filo.writelines([“welcome\n”,”to python\n”,”Byee\n”])

Filo.close()

Filo = open(‘C:\san\sample.txt’,’r’)

Filo.readline(6)

File.readline()

Output

Welcom

Welcome\n

readlines()

  • The method reads all the lines and returns the lines along with newline as a list of strings.

Filo = open(‘C:\san\sample.txt’,’w’)

Filo.writelines([“welcome\n”,”to python\n”,”Byee\n”])

Filo.close()

Filo = open(‘C:\san\sample.txt’,’r’)

Filo.readlines()

Output

[‘welcome\n’, ‘to python\n’, ‘byee\n’]

seek()

  • This method is used to position the file object at a particular position in a file.
  • file_object.seek(offset [, reference_point])
  • offset is the number of bytes by which the file object is to be moved.
  • reference_point indicates the starting position of the file object.
  • Reference point can have any of the following values:
    • 0 – beginning of the file
    • 1 – current position of the file
    • 2 – end of file
  • By default, the value of reference_point is 0

tell() method

  • This function returns an integer that specifies the current position of the file object in the file.
  • file_object.tell()

Binary File

  • Binary files stores data as sequence of bytes which do not represent ASCII value of characters.
  • Binary files are not human readable
  • We need specific software to read and write content of Binary file
  • Binary file generally represents actual contents like images, videos, executable files etc.
  • Change in single bit can corrupt a complete binary file
  • Errors occurred in Binary file is very difficult to remove

Pickle Module

  • The module Pickle is used for serializing and de-serializing any Python object structure.
  • Serialization is the process of transforming data or an object in memory (RAM) to a stream of bytes called byte streams.
  • De-serialization or unpickling is the inverse of pickling process where a byte stream is converted back to Python object. 
  • The pickle module deals with binary files. Here, data are not written but dumped and similarly, data are not read but loaded.
    • dump()
    • load()

Serialization

Dump()

import pickle

listvalues=[1,”Geetika”,’F’, 26]

fileobject=open(“mybinary.dat”, “wb”)

pickle.dump(listvalues,fileobject)

fileobject.close()

Load ()

import pickle

print(“The data that were stored in file are: “)

fileobject=open(“mybinary.dat”,”rb”)

objectvar=pickle.load(fileobject)

fileobject.close()

print(objectvar)

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!