0% found this document useful (0 votes)
196 views8 pages

2nd Puc Notes State Board

Chapter 2 discusses file handling in Python, explaining the types of files (text and binary), their storage, and how to read/write them. It covers file operations, modes, and the use of the 'with' clause for file management, as well as serialization and deserialization using the Pickle module. Key methods such as read(), write(), seek(), and tell() are also detailed.

Uploaded by

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

2nd Puc Notes State Board

Chapter 2 discusses file handling in Python, explaining the types of files (text and binary), their storage, and how to read/write them. It covers file operations, modes, and the use of the 'with' clause for file management, as well as serialization and deserialization using the Pickle module. Key methods such as read(), write(), seek(), and tell() are also detailed.

Uploaded by

yogeshgowda2366
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CHAPTER -2 FILE HANDLING IN PYTHON

[Link] can a python file contain permanently?


A python file can contain data entered and output generated permanently in a file .

[Link] is a file in python?


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

[Link] are the types of files in python?


There are two types of files in python
[Link] File
A text file consists of human readable characters, which can be opened by any text editor.
[Link] File
Binary files are made up of non-human readable characters and symbols, which require specific programs to
access its contents.

[Link] is the data in the text file stored /processed?


a. A text file can be understood as a sequence of characters consisting of alphabets, numbers and other special
symbols. Files with extensions like .txt, .py.
b. The data is stored in a sequence of bytes consisting of 0s and 1s. In ASCII, UNICODE or any other encoding
scheme, the value of each character of the text file is stored as bytes.
c. While opening a text file, the text editor translates each ASCII value and shows us the equivalent character
that is readable by the human being. For example, the ASCII value 65 (binary equivalent 1000001) will be
displayed by a text editor as the letter ‘A’ since the number 65 in the ASCII character set represents ‘A’.

[Link] is EOL?

EOL refers to “End of Line “


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).

[Link] is binary file?


● Binary files are also stored in terms of bytes (0s and 1s).
● These bytes do not represent the ASCII values of characters.

[Link] can a binary file be Accessed ?

These files are not human readable. Thus, trying to open a binary file using a text editor will show some
garbage values. We need specific software to read or write the contents of a binary file.

[Link] can't errors be removed from binary files?


It is difficult to remove any error which may occur in the binary file as the stored contents are not human readable.

9 .Can python programs read and write binary files ?


Yes
[Link] are the different operations that are available on files using python?
The below are the functions of file using python
[Link] and opening a file.
[Link] data in a file
[Link] data from a file .
[Link] a file

11. Which module of python has functions related to file operations?

Io module of python can handle all the functions of python.

12. What is a file handler ?

File handler is the file object which is used for transferring the file to and fro from the file.,it is generally a
userdefined variable

[Link] are the different basic file modes in python file handling ? what are symbols used >

[Link] mode - r
[Link] mode - w (it also creates the file if the file does not exist )
[Link] mode - a ( appends the data at the end of the file )
[Link] mode - b
[Link] mode + - used for both reading writing

[Link] how we can open and read the contents of a txt file using a python program?

The python code used to open and read the contents of a txt file in python is

Description
1.“Fo” is the file handler.
[Link] () is built in method used to open the file which accepts two arguments the file name ie “[Link]” and
the file mode ie ‘r’
3.

15. What is the default file mode in python?

The default file mode is read mode ie indicated by “r”

16. What is closing of file in file handling in python ?


Once we are done with the read/write operations on a file, it is a good practice to close the file. Python provides
a close() method to do so. While closing a file, the system frees the memory allocated to it. The syntax of close()
is: file_object.close()

[Link] the file handler reused after closing the file?

Yes it is reused to another file

[Link] is with clause ? how is it used with files in python?

In Python, we can also open a file using with clause.

The syntax of with clause is:


with open (file_name, access_mode) as file_ object:

19. Write a example program to understand the use of with clause in python ?

20. What are the different ways of writing a file ?

We can write a file using two methods


[Link] the file in write mode ie “w”
[Link] append mode

[Link] is the difference between write mode and append mode ?

Write mode Append mode

When a file is written using write mode the When i file is written in append mode the new
existing content is erased out and the new data is added at the end of previous data
content is written.

[Link] are the different built in methods used to write the data in a file ?

The two built in methods used to write data in a file are


[Link]() - for writing a single string
write() method takes a string as an argument and writes it to the text file. It returns the number of
characters being written on single execution of the write() method. Also, we need to add a newline
character (\n) at the end of every sentence to mark the end of line. The file must be mandatorily closed
Example
d=open("[Link]","w")
[Link]("checking if the content is rewritten \n is the new line added??? \n yesss")
[Link]()

2. writelines() - for writing a sequence of strings


This method is used to write multiple strings to a file. We need to pass an iterable object like lists,
tuple, etc. containing strings to the writelines() method.

[Link] are the different ways to read a file in python file handling?

There are three ways of reading file in python


[Link]() method -This method is used to read a specified number of bytes of data from a data file.
The syntax of read()
method is: file_object.read(n)
[Link]([n]) method - This method reads one complete line from a file where each line terminates with a
newline (\n) character. It can also be used to read a specified number (n) of bytes of data from a file but
maximum up to the newline character (\n).

[Link]() -

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

24. How is data accessed from a file using read and write operation in python ?

Data is accessed sequentially while using read and write operation in python.

[Link] we access data randomly using the concept of file in python ?

Yes data can be accessed randomly using the concept of seek() and tell() functions in python.
26. Explain seek() and tell() method in detail with example ?

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

seek()
This method is used to position the file object at a particular position in a file. 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. That is, with
reference to which position, the offset has to be counted. It 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,
Syntax :
file_object.seek(offset [, reference_point])

[PROGRAM 2.2 TO BE DISCUSSED IN CW]

27. What is pickle module in python?

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.

[Link] is serialization ?
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.

29. What is deserialization?

De-serialization or unpickling is the inverse of pickling process where a byte stream is converted back to
Python object.

[Link] is the pickle module used in python?

The Pickle Module must be imported to load and dump data, because The pickle module deals with binary
files. Here, data are not written but dumped and similarly, data are not read but loaded. The pickle module
provides two methods - dump() and load() to work with binary files for pickling and unpickling, respectively.

[Link] dump() method with syntax


dump() is used to convert (pickling) Python objects for writing data in a binary file. The file in which data are to
be dumped, needs to be opened in binary write mode (wb).
Syntax of dump() is as follows:
dump(data_object, file_object)

Pickling data in Python


import pickle
listvalues=[1,"Geetika",'F', 26]
fileobject=open("[Link]", "wb")
[Link](listvalues,fileobject)
[Link]()

32 . What is load() method ? how does it work?

This method is used to load (unpickling) data from a binary file. The file to be loaded is opened in binary read
(rb) mode.
Syntax of load() is as follows:
Store_object = load(file_object)

Unpickling data in Python


import pickle
print("The data that were stored in file are: ")
fileobject=open("[Link]","rb")
objectvar=[Link](fileobject)
[Link]()
print(objectvar)

PROGRAM 2-8 TO BE DONE DURING CLASS

***********************************************************************************************

You might also like