KEMBAR78
Python data file handling | PPTX
Data File Handling
A file is a bunch of bytes stored on some storage device like hard disk, pen drive etc.
Files help in storing information permanently, they store data pertaining to a specific application.
These data files can be strored in two ways:
Text Files Binary Files
it stores information in ASCII or UNICODE characters.
Each line of text is terminated with special character known
as EOL(‘n’)
It contains information in the same format in which the
information is held in memory. (hexadecimal for us )
EOL is (‘rn’)
Internal translation take place when EOL character is read
or written. so text files are slower for programs to read and
write
No Internal translation take place when EOL character is
read or written. so binary files are faster and easier for
program to read and write.
it is most suitable when file need to be read by people. it is most suitable when file dont’ need to be read by people.
Standard Streams / Standard Devices - ( standard devices are implemented as standard streams)
1. stdin -> Standard input device -> keyboard -> reads from keyboard
2. stdout -> Standard Output device -> monitor-> writes to monitor
3. stderr -> Standard error device -> monitor -> writes only errors to monitor.
In python import sys module let us access these standard devices.
so instead of print(“hello world”) we can use sys.stdout.write(“hello world”)
Opening and closing File-
To work with a file within a program you need to open it.
While opening a file you need to mention appropriate
mode (r/w/a)
Function
/
Method
syntax- Example-
open()
(is a
function
)
fileobject=open(filename)
fileobject=open(filename,
mode)
‘’’establishes link between
fileobject/filehandle and a file on
disk.’’’
fi=open(“Input.txt”) OR fi=open(“..Input.txt”) OR fi=open(“d:programsInput.txt”)
fo=open(“Output.txt”,”w”) OR fo=open(“..Output.txt”,”w”) OR
fo=open(“d:programsOutput.txt”,”w”)
file object path to file mode
with open (filename,mode) as fileobject: with open (“Input.txt”,”r”) as fi: with open (“Output.txt”,”a”)
as fo:
file manipulation statement print(f.read(1)) fo.write(“Reaches to
end”)
close()
(is a
method
fileobject.close()
‘’’ breaks the link of fieobject and
file on disk.’’’
fi.close()
fo.close()
File modes
Text
File
Mode
Binary
File
Mode
Descripti
on
r w a Create a file
if does not
exist
Position of file pointer
on file opening
‘r’ ‘rb’ only read ✔ ✖ ✖ beginning of file
‘w’ ‘wb’ only write ✖ ✔ erases/truncates contents of the file
and start writing from beginning
(Overwrites the file)
✔ beginning of file
‘a’ ‘ab’ append
(only write)
✖ ✔ write only at end of the contents of the
existing file(retains old contents)
✔ if file doesn’t exist-
beginning of file
if file exists- end of the file
‘r+’ ‘r+b’ /
‘rb+’
read &
write
✔ ✔ ✖ beginning of file
‘w+’ ‘w+b’ /
‘wb+’
read &
write
✔ ✔ erases/ truncates contents of the file
and start writing from
beginning(Overwrites the file)
✔ beginning of file
‘a+’ ‘a+b’ /
‘ab+’
read &
write
✔ ✔ write only at end of the contents of the
existing file(retains old contents)
✔ if file doesn’t exist-
beginning of file
if file exists- end of the file
(Sequentially)Reading Writing Files
Method syntax Description example output
read() fileobject.read([n]) if n is specified reads ‘n’ bytes from
file otherwise reads entire file
returns read bytes in the form of
string
1) file=open('......story.txt','r')
l=file.read()
print(l)
print(type(l))
1) file=open('......story.txt','r')
l=file.read(6)
print(l)
print(type(l))
Jingle Bell Jingle bell
Jingle all the way
Oh! what fun it is to ride on
one horse open sledge.
<class 'str'>
Jingle
<class 'str'>
readline() fileobject.readline([n])
if n is specified reads n bytes from
file,otherwise reads a line of file from
current position,
returns read bytes in form of string
1) file=open('......story.txt','r')
l=file.readline() # add strip()
print(l)
print(type(l))
1) file=open('......story.txt','r')
l=file.readline(6)
print(l)
print(type(l))
Jingle Bell Jingle bell
<class 'str'>
Jingle
<class 'str'>
readlines(
)
fileobject.readlines()
reads all lines and returns them in a
form of list
1) file=open('......story.txt','r')
l=file.readlines()
print(l)
print(type(l))
['Jingle Bell Jingle belln',
'Jingle all the wayn', 'Oh!
what fun it is to ride onn',
'one horse open sledge.']
Randomly reading writing files
Method syntax description example Output
tell() fileobject.tell() returns an integer giving the current
position of object in the file. The integer
returned specifies the number of bytes
from the beginning of the file till the
current position of file object.
f=open('story.txt','r+')
print(“position:”,f.tell()
)
fdata = f.read(7)
print (fdata)
print(“position:”,f.tell()
)
f.close()
0
Jingle
8
seek() fileobject.seek(offset [, from_what])
here
offset is reference point from_where
to get the position.
return position the file object at
particular place in the file.
list of from_where values:
Value reference point
0 beginning of the file
1 current position of file
2 end of file
*default value of from_where is 0, i.e.
beginning of the file
f=open('story.txt','r+')
f.seek(7)
fdata = f.read(7)
print fdata
f.close()
Let's read the second
word from the test1 file
created earlier. First
word is 5 alphabets, so
we need to move to 5th
byte. Offset of first byte
starts from zero.
Bell
We know that the methods provided in python for writing / reading a file works with string parameters.
So when we want to work on binary file, conversion of data at the time of reading, as well as writing is
required.
Pickle module can be used to store any kind of object in file as it allows us to store python objects with their
structure.
So for storing data in binary format, we will use pickle module.
First we need to import the module.
It provides two main methods for the purpose, dump and load.
Pickle Method Description Syntax Example:
dump() - to write the object in file, which
is opened in binary access
mode.
pickle.dump
(object, fileobject)
import pickle
l = [1,2,3,4,5,6]
file = open('list.dat', 'wb')
pickle.dump(l,file)
load() use pickle.load() to read the
object from file which is opened
in binary access mode.
object =
load(fileobject)
ifile = open('myfile.dat', 'rb')
L = pickle.load(ifile)
ifile.close()
print (L)

Python data file handling

  • 1.
    Data File Handling Afile is a bunch of bytes stored on some storage device like hard disk, pen drive etc. Files help in storing information permanently, they store data pertaining to a specific application. These data files can be strored in two ways: Text Files Binary Files it stores information in ASCII or UNICODE characters. Each line of text is terminated with special character known as EOL(‘n’) It contains information in the same format in which the information is held in memory. (hexadecimal for us ) EOL is (‘rn’) Internal translation take place when EOL character is read or written. so text files are slower for programs to read and write No Internal translation take place when EOL character is read or written. so binary files are faster and easier for program to read and write. it is most suitable when file need to be read by people. it is most suitable when file dont’ need to be read by people. Standard Streams / Standard Devices - ( standard devices are implemented as standard streams) 1. stdin -> Standard input device -> keyboard -> reads from keyboard 2. stdout -> Standard Output device -> monitor-> writes to monitor 3. stderr -> Standard error device -> monitor -> writes only errors to monitor. In python import sys module let us access these standard devices. so instead of print(“hello world”) we can use sys.stdout.write(“hello world”)
  • 2.
    Opening and closingFile- To work with a file within a program you need to open it. While opening a file you need to mention appropriate mode (r/w/a) Function / Method syntax- Example- open() (is a function ) fileobject=open(filename) fileobject=open(filename, mode) ‘’’establishes link between fileobject/filehandle and a file on disk.’’’ fi=open(“Input.txt”) OR fi=open(“..Input.txt”) OR fi=open(“d:programsInput.txt”) fo=open(“Output.txt”,”w”) OR fo=open(“..Output.txt”,”w”) OR fo=open(“d:programsOutput.txt”,”w”) file object path to file mode with open (filename,mode) as fileobject: with open (“Input.txt”,”r”) as fi: with open (“Output.txt”,”a”) as fo: file manipulation statement print(f.read(1)) fo.write(“Reaches to end”) close() (is a method fileobject.close() ‘’’ breaks the link of fieobject and file on disk.’’’ fi.close() fo.close()
  • 3.
    File modes Text File Mode Binary File Mode Descripti on r wa Create a file if does not exist Position of file pointer on file opening ‘r’ ‘rb’ only read ✔ ✖ ✖ beginning of file ‘w’ ‘wb’ only write ✖ ✔ erases/truncates contents of the file and start writing from beginning (Overwrites the file) ✔ beginning of file ‘a’ ‘ab’ append (only write) ✖ ✔ write only at end of the contents of the existing file(retains old contents) ✔ if file doesn’t exist- beginning of file if file exists- end of the file ‘r+’ ‘r+b’ / ‘rb+’ read & write ✔ ✔ ✖ beginning of file ‘w+’ ‘w+b’ / ‘wb+’ read & write ✔ ✔ erases/ truncates contents of the file and start writing from beginning(Overwrites the file) ✔ beginning of file ‘a+’ ‘a+b’ / ‘ab+’ read & write ✔ ✔ write only at end of the contents of the existing file(retains old contents) ✔ if file doesn’t exist- beginning of file if file exists- end of the file
  • 4.
    (Sequentially)Reading Writing Files Methodsyntax Description example output read() fileobject.read([n]) if n is specified reads ‘n’ bytes from file otherwise reads entire file returns read bytes in the form of string 1) file=open('......story.txt','r') l=file.read() print(l) print(type(l)) 1) file=open('......story.txt','r') l=file.read(6) print(l) print(type(l)) Jingle Bell Jingle bell Jingle all the way Oh! what fun it is to ride on one horse open sledge. <class 'str'> Jingle <class 'str'> readline() fileobject.readline([n]) if n is specified reads n bytes from file,otherwise reads a line of file from current position, returns read bytes in form of string 1) file=open('......story.txt','r') l=file.readline() # add strip() print(l) print(type(l)) 1) file=open('......story.txt','r') l=file.readline(6) print(l) print(type(l)) Jingle Bell Jingle bell <class 'str'> Jingle <class 'str'> readlines( ) fileobject.readlines() reads all lines and returns them in a form of list 1) file=open('......story.txt','r') l=file.readlines() print(l) print(type(l)) ['Jingle Bell Jingle belln', 'Jingle all the wayn', 'Oh! what fun it is to ride onn', 'one horse open sledge.']
  • 5.
    Randomly reading writingfiles Method syntax description example Output tell() fileobject.tell() returns an integer giving the current position of object in the file. The integer returned specifies the number of bytes from the beginning of the file till the current position of file object. f=open('story.txt','r+') print(“position:”,f.tell() ) fdata = f.read(7) print (fdata) print(“position:”,f.tell() ) f.close() 0 Jingle 8 seek() fileobject.seek(offset [, from_what]) here offset is reference point from_where to get the position. return position the file object at particular place in the file. list of from_where values: Value reference point 0 beginning of the file 1 current position of file 2 end of file *default value of from_where is 0, i.e. beginning of the file f=open('story.txt','r+') f.seek(7) fdata = f.read(7) print fdata f.close() Let's read the second word from the test1 file created earlier. First word is 5 alphabets, so we need to move to 5th byte. Offset of first byte starts from zero. Bell
  • 6.
    We know thatthe methods provided in python for writing / reading a file works with string parameters. So when we want to work on binary file, conversion of data at the time of reading, as well as writing is required. Pickle module can be used to store any kind of object in file as it allows us to store python objects with their structure. So for storing data in binary format, we will use pickle module. First we need to import the module. It provides two main methods for the purpose, dump and load. Pickle Method Description Syntax Example: dump() - to write the object in file, which is opened in binary access mode. pickle.dump (object, fileobject) import pickle l = [1,2,3,4,5,6] file = open('list.dat', 'wb') pickle.dump(l,file) load() use pickle.load() to read the object from file which is opened in binary access mode. object = load(fileobject) ifile = open('myfile.dat', 'rb') L = pickle.load(ifile) ifile.close() print (L)