File handling in python

Files:- files are permanent storage of related data in ROM i.e. hard disk of Computer system. RAM can not save data because it's volatile and lose its data when computer is off.

File open and close:-

python has open function to open any file. It's has certain modes to specify how to open a file. Let us see example below.

File=open('myfile.txt','r')

File2=open('c:/myfile2.txt,'r')

Above code shows we are opening myfile.txt . We are opening with r mode that is read mode. It is default mode, it means if we do not specify mode then file will open in read mode only. If file does not exist in current directory or path described in open function then program shows error.

Modes of files opening in python:-

r : means open a file in read modeprogram fails if      file do not exist in described file location or path.

w: means open a file in write mode. It creates a             file if do not exist. It will delete existing file data       if file already exists.

x: only creates a file. Gives error if file already exist.

a: creates a file if it doesn't exist. Opens a file in append mode. Adds the data from the end of the file.

t: opens a file in text mode. It is default mode. It means if no mode argument is given in open function then then it is considered as r or rt mode.

b: opens file.in binary mode. It's deals with data in byte form is. Basic form of data on system. It's is used when dealing with image, executable files and data transfer operations on cross platforms.

+: Opens a file for updation. It means reading and writing.

Closing file: when we are done with file operations then we should safely close the file. We use close function to close the file. See below example.

File.close().

Best way is to use with keyword for file operations.by using with keyword file will close automatically when the code inside with block ends. See example.below

With open('myfile2.txt','a'):

     # file manipulation code

 

Writing files in python:-writing files in python is a done using write function. We should be careful of modes described above while writing to files. Writing to files is done using write function. See example below.

File=open('myfile.txt','a')

File.write('my name is vijay')

this function returns the number of characters written to file. Number of bytes when writing in binary mode.

 

Reading files in python:-

We can read files using read function. We can specify the no of characters to read or we can read whole line or we can read whole file. See below.examples

File.read(4) #read first 4 characters.

File.readline() #reads a single line untile \n character(end of line)

File.read() # reads whole file till end of file.

 

click below file handling example program link to see the full example.

file handling example in python

 

File functions and methods :-now try out various file functions yourself. 

 

standard input, output and error streams.

python provides us with three file like handles or objects. These are connected to standard input devices like keyboard, standard output devices like monitor or console and error display devices like monitor.

Sys module provides us 3 objects stdin,stdout,stderr to handle these 3 functionalities. We can use these objects to manipulate our functionalities.

Stdin connected to keyboard by defualt. user program gets input from this file handle object.

stdout connected to display by default. user program writes normal output and data to this file handle.

stderrr is connected to display by default. user program writes exceptions and error information to this file handle.

Let us check how we can work with these objects to work with the input and output of our program.

a basic example:

import sys # to reference sys library of python

stdinput = sys.stdin

# Keeps reading from stdinput object and stops if the word 'stop' comes in new line followed by enter in input from keyboard
# This loop will not terminate, since sys.stdin is open
for line in stdinput:
# Remove trailing newline characters using strip()
if 'stop' == line.strip():
   print('now Terminating the program')
   exit(0)
else:
   print('input from sys.stdinput-->',line +'(write stop in new line and press enter to exit)')
 
stdout Example below.
import sys
stdout_file = sys.stdout
input = ['Hi', 'please visit computersforschool.joomla.com', 'bye'] #input data from program
for data in input:
    # Print to stdout handle(i.e. stdout_file object here)
    stdout_file.write(data+'\n') # writes to display screen.
 
Now we will see stderr file handle example, it is similar to stdout but only display errors and exceptions on screen.
 import sys

stdout_file = sys.stdout
stderr_file = sys.stderr

input = ['Hi', 'how are you today', 'bye!!']

for data in input:
# Print to stdout
stdout_file.write(data + '\n') # written to screen
# Try to divide data with number. Raises an exception
try:
  data = data/100
# Catch all exceptions
except:
  stderr_file.write('error, string divide by number not possible!\n')# message written to screen
 
on above example data is written to screen and exception is also written to screen by stderr file handle object(stderr_file).
 
 Seek and tell methods in python
seek():-> seek methods positions the cursor after the required number of characters in a text file(space is also a character). then we can read and write from there onwards.it takes a number argument. example below
seek(10) will place cursor to the 11th character position.
 
tell() :-> tell method returns the position in number where cursor is placed(after number of characters) at that moment of time.
 
Absolute and relative paths in file:
Absolute path is the full qualified location of a file on hard disk(memory). for example
C:/program files/python/example1.py 
Relative path is a location of a file program has to go through in relation to the current directory of the program. for example '/python/example1.py' , in this we are considering that we are writing program in 'program file' folder or directory. so no need to write full path of the the file location
see example below of both
 
file=open('C:/program files/python/example1.py') #opening file using absolute path
file=open('/python/example1.py') #opening file using relative path.
 
Let us check one more code example
 
os.path.exists('c:/program files/python/example 1.py')
 
Above line returns True, if file exist. It is example of absolute path. Similarly you can check for relative path.
 
Working with binary files :-
we will discuss now binary files in python. Binary files are executable data in system. Binary files are written in bytes. We can not understand binary data, because it is machine code. Examples of binary files are image files, executable code files, music files, video files.
 
Open and close in binary files is same, but there is change in mode arguments.we have to write character b in mode argument in addition to r,w,a,+ characters.
 
Let us take an example of reading in binary files.
#open binary file for reading, error if not exist
File=open('myfile.bin','rb')
 
#read string of bytes from file.
Data=File.read()
 
#output will be in hexadecimal character representation of data
print(Data)
 
#unicode of characters will be shown as integer
print(Data[2])
 
#we can use bin function to show binary representation of character.
print(bin(Data[3]))
f.close()
 
let us take an example of update of binary file.
# working with Binary files. Writing / reading a tuple containing character strings

#The specified tuple with strings
data = ( 'hi', 'there', 'visitor', 'whatsup')

#Write tuple T to file 'myfile5.bin'
#Open file for writing
file = open('myfilebin.bin', 'a+b')

# Tuple data input
for item in data:
   byte = (item + '\n').encode() # converting (str + '\n') to bytes
   file.write(byte) # write byte to file mentioned above

# Read data from file
for line in file:
   string = line.decode() #convert bytes to str
   print(string)
file.close()
 
 
Pickle module in Python3 :
Python pickle module is used for serializing and de-serializing a Python object structure. what is serializing ? it means converting data structures(list,tuple,dictionary) and objects into byte stream. this process is known as pickling and reverse of this is known as Unpickling.
 
  • dump() – This function is called to serialize an object/structure hierarchy. This is used to write a pickled representation of obj/data structure to the open file handle object.pickle.dump(objfileprotocol=None*fix_imports=Truebuffer_callback=None)
  • Write the pickled/serialized representation of the object obj to the open file object file, protocol stands for integer argument that can be used to pass protocol version. default protocol value is now 4 from python 3.8 onwards. if fiximports=True  and protocol is less than 3, then pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2. if buffer_callback is None (the default), it means buffer views are serialized into file as part of the pickle stream.
  • It is an error if buffer_callback is not None and protocol is None or smaller than 5.

let us take an example:

import pickle #import pickle module
  
def store_Data():
    # initializing data to be stored in database
    spice = {'key' : 'spice', 'name' : 'pepper spice'}
 
    dish = {'key' : 'dish', 'name' : 'dish palak'}
   
  
    # database
    db = {}
    db['spice'] = pepper
    db['dish'] = palak
      
    # Its important to use binary mode
    file = open('example', 'ab')
      
    # source, destination
    pickle.dump(db,file)                     
    file.close()
  
def load_Data():
    # for reading now binary mode is required
    file = open('example', 'rb')     
    db = pickle.load(file)
    for keys in db:
        print(keys, '=>', db[keys])
    dbfile.close()
  
if __name__ == '__main__':
    store_Data()
    load_Data()
  • load – This function is called to de-serialize a byte stream and returns data in original form so that we can read it. see use of load function above.

Search and Append data in binary file:

for appending data in binary file we have to open the file in 'ab' mode. means opening binary file in append mode. and to update  the file we have open it in 'a+b' mode. to search the file we can make use of seek() function to position the cursor in file. we used it to position from starting of file. below example will is self explanatory.

import pickle
file=open('mydata.data','a+b')#open binary file in append and update mode 'a+b'
record=[]
choice='y'
while(choice!='n'): #add records until choice is 'n'
      print("add data now..")
      name=input('enter name : ')
      code=int(input('enter code no.'))
      record.append([name,code])
      pickle.dump(record,file)
      choice=input('want to enter more data? press y for yes n for no')#decision
try:
      search_code=int(input('enter the code to search'))# input search code in integer
      pos=file.seek(0) #position cursor in starting
      found=False
      while True:
            data=pickle.load(file)
            for record in data:
                 if(record[1]==search_code):#check code at index 1 in records
                     print('name is : ',record[0]) #print name at index 0 in record
                     found=True #record found flag
                     break
except Exception:
      file.close()
if(found==True):
    print('Record found..') #found message
else:
    print('record not found..') #not found message

 

Similary you can try update and delete of records yourself.