0% found this document useful (0 votes)
7 views79 pages

File Handling

Uploaded by

papneja.aniket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views79 pages

File Handling

Uploaded by

papneja.aniket
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

A file object(also called as file-handle) is a

reference to a file on disk. It opens and makes it


available for a number of different tasks. It is very
important as through file object only, a python
program can work with files stored on hardware.
The default file open mode is read mode.
It is important to close files as soon as you have finished your work with file.
Opened file is closed by calling the close() method of its file objects.
A close() function breaks the link of file object and the file on the disk. After close(), no
task can be performed on that file through the file-object(or file-handle)
With open(“filename.txt”,”r”) as f

“The with statement simplifies exception handling by encapsulating common


preparation and cleanup tasks.” In addition, it will automatically close the file. The
with statement provides a way for ensuring that a clean-up is always used.
a(append) - Opens a file for appending. The file
pointer is at the end of the file if the file exists.
That is, the file is in the append mode. If the file
does not exist, it creates a new file for writing.
a+ - Opens a file for both appending and reading.
The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If
the file does not exist, it creates a new file for
reading and writing.
tell() – This function returns the current
position of file pointer in the file.
For ex:-
f.seek(30,0) = Will place the file pointer at 30th byte from the beginning of the file
f.seek(30,1)= Will place the file pointer at 30th byte ahead of current file pointer
position
f.seek(-5,1)= Will place the file pointer at 5 bytes behind(backward direction)from
current file pointer position
f.seek(-30,2) = Will place the file pointer at 30th byte behind(backward direction)from
end of file.
WAP to copy data from one file to another
file
NOTE:- similarly you canovert to Titlecase,lowercase, and togglecase,etc.
Q1.
Q2.
Q3.
Q4.
Binary File Handling
Python considers everything as an object. So, all data types including list, tuple, dictionary,
etc. are also considered as objects. During execution of a program, we may require to store
current state of variables so that we can retrieve them later to its present state.

Suppose you are playing a video game, and after some time, you want to close it. So, the
program should be able to store the current state of the game, including current level/stage,
your score, etc. as a Python object. Likewise, you may like to store a Python dictionary as an
object, to be able to retrieve later.

To save any object structure along with data, Python provides a module called Pickle. The
module Pickle is used for serializing and de-serializing any Python object structure.

Pickling is a method of preserving food items by placing them in some solution, which
increases the shelf life. In other words, it is a method to store food items for later
consumption
• Serialization is the process of transforming data or an object in memory
(RAM) to a stream of bytes called byte streams. These byte streams in a
binary file can then be stored in a disk or in a database or sent through a
network. Serialization process is also called pickling.
• De-serialization or unpickling is the inverse of pickling process where a
byte stream is converted back to Python object.
READ
EOF error
• This error is likely to occur when:
• we fail to declare a statement for loop
(while/for)
• we omit the closing parenthesis or curly
bracket in a block of code.
• If file is not properly closed
Write
append
Search
Update
delete
Opening/Closing csv files
• Open a csv file:
f=open(“stu.csv”,”w”)
Or
f=open(“stu.csv”,”r”)
• Close a csv file:
• f.close()
Since CSV files are delimited flat files and before
writing onto them, the data must be in a CSV-
Writeable-delimited-form, it is important to
convert the received user data into the form
appropriate for the csv files. This task is performed
by the writer object. The data row written to a
writer object(written using writerow() or
writerows() function) gets converted to csv
writeable delimited form and then written on to
the linked csv file on the disk.
Writerow() without newline and delimiter
Writerow() with newline
Writerow() with delimiter
Follow the steps given below to space out the
columns in spreadsheet:
1. Click->data->from text(import python file of
csv code)->locate csv file ->import
2. Click next
3. Check semicolon checkbox ->finish
4. Choose option newworksheet
Writerows()
Reading in a csv files
Reading from a csv file involves loading of a csv
files’s data, parsing it(i.e, removing its
delimitation), loading it in a python iterable
and then reading from this iterable.
Csv,reader() – returns a reader object which
loads data from csv file into an iterable after
parsing delimited data
The csv.reader object does the opposite of
csv.writer object. The csv.reader object loads
data from the cvs file, parses it, i.e., removes
the delimiters and returns the data in the form
of a python iterable where from you can fetch
one row of data at a time.

You might also like