File handling in python
- Details
- Category: Uncategorised
- Published: Wednesday, 23 June 2021 11:27
- Written by Super User
- Hits: 1103
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 mode. program 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 pythonstdinput = 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)')
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.
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
#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()
- 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
(obj, file, protocol=None, *, fix_imports=True, buffer_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.