[go: up one dir, main page]

0% found this document useful (0 votes)
3 views4 pages

CH 5

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

S.

Text File Binary File


NO
1. A text file stores information in the form of a stream of A binary file stores information in the form of a stream of
ASCII or Unicode characters. bytes. It stores information in the same format in which the
information is held in memory.

2. Each line of text is terminated (delimited) with a special There is no delimiter for a line in binary files.
character known as EOL (End Of Line) character.

3. In text files, some internal translations take place when There is no translation in binary files.
this EOL character is read or written.

4. In Python, by default, EOL is the newline character (‘\n’) There is no delimiter for a line in binary files.
or the carriage –return newline combination (‘\r\n’).

5. Text files are in human readable form. Binary files are not in human readable form.
6. Common extension: .txt Common extension: .dat

Q) What is file-object?

Ans:- Python’s open( ) function creates a file object (also known as file-handle) which serves as a
link to a file residing in your computer and makes it available for a number of different task.
For example: myfile = open(“data1.text”, ”r”)

NOTE
With r you can use single slashes in pathnames. Myfile = open(r”C:\temp\data1.txt”, “r”)
The prefix r makes it raw string that means there is no special meaning attached to any character.

Q) What is file-mode?
Ans:- A file-mode governs the type of operations (read/write/append) possible in the opened file.
i.e., it refers to how the file will be used once it is opened. It is specified in the open( ) function.
Syntax - <fileobject> = open(<path>, <file-mode>)

S.NO open( ) close( )


1. The open( ) is used to open a file. The close( ) is used to close a file.
2. open( ) is a built-in function. close( ) is a method used with file-
handle/object.
3. It creates file object which serves as a link to a file When we’re done working with a file, we
residing on your computer and makes it available should always close it using close( ) to
for a number of different tasks (e.g., reading from prevent data loss, and unnecessary
it, writing to it, appending data to it). blockage of memory.
4. Eg.: Myfile = open(“data1.txt”,”w”) Eg.: Myfile.close( )
Refer the notepad file below –

S.NO. read( ) readline( ) readlines( )


1. <fileobject>.read([n]) <fileobject>.readline([n]) <fileobject>.readlines( )
2. Reads atmost n bytes; if no n is Reads a line of input; if n is Reads all the lines; if n is
specified then it reads the specified then it reads atmost n specified then it reads
entire file. bytes. i.e., when n is specified atmost n lines.
readline( ) behaves as read( ).
3. Return the read bytes in the Returns the read bytes in the Returns the read bytes
form of a string. form of a string ending with ‘\n’ in the form of a list.
character.
EXAMPLE:-
NOTE
If you want to perform only one task on the open file, you can combine file( ) or open( ) with the file-
object function as shown below:
>>> file(r”C:\temp\data1.txt”,”r”).read(30)
>>> open(r”C:\temp\data1.txt”,”r”).readline( )

Q) What is the use of flush( ) ?


Ans: - The flush( ) function forces the writing of data on disc still pending in the output buffer.

Removing the EOL ‘\n’ character from Removing the leading whitespaces from
the line read from the file the line read from the file

Myfile = file(“data1.txt”,”r”) line = file(“data.txt”, “r”).readline( )


line = Myfile.readline( ) line = line.lstrip()
line = line.rstrip(‘\n’)

S.NO. ABSOLUTE PATHNAMES RELATIVE PATHNAMES

1. Absolute pathnames mentions the path Relative pathnames mention the paths
from the topmost level of the directory relative to current working directory. The
structure. dot(.) denotes the current directory and two
dots(..) denotes the parent directory.
2. C:\Users\HP\ GAMES\SAO\NOTES.DOC ..\SAO\NOTES.DOC

Q) What do you mean by serialisation(pickling) and unpickling?


Ans:- Serialisation (also called Pickling) is the process of converting Python object hierarchy
into a byte-stream. Unpickling is the reverse of pickling where a byte stream is converted back
into an object hierarchy.
Pickling is done by using dump( ) method and Unpickling is done by using load( ) method.
Syntax: pickle.dump(<object-to-be-written>,<fileobject>)
<var> = pickle.load(<fileobject>)
DEALING WITH EOF ERROR
pickle.load( ) raises EOFError when you reach the end-of-file.
Thus, we must use pickle.load( ) enclosed in try and except statements. The try and except
statements together can handle runtime exceptions.
The with statement is a compact statement which combines the opening and processing of file
along with inbuilt exception handling. The with statement will close the file automatically after
with block is over.
(Eg):-
try:
while True:
<var> = pickle.load(<fileobject>)
# other statements like print, while loop etc.,
except EOFError:
<fileobject>.close()
(or)
with open(<path>,<filemode>) as <fileobject>:
<var> = pickle.load(<fileobject>)
# other statements like print, while loop etc.,
# no need for close( ) method as with statement automatically closes the file after with block.

S.NO. tell( ) seek( )


1. The tell( ) function returns the current The seek( ) function places the file pointer at
position of file pointer in an open file. the specified position in an open file.
2. Syntax: <fileobject>.tell( ) Syntax: <fileobject>.seek(<offset>,<mode>)

Where Offset -> number of bytes


Mode -> 0 – beginning of the file (default mode)
1 – current position of the file pointer
2 – end of the file
Q) What are csv files? Why are they popular for data storage?
Ans:- CSV files are Comma Separated Values files. These are the delimited files where mostly
comma is used to separate the values stored. These files can also have delimiter other than
comma but comma is the default delimiter. These files take the extension as .csv .
They are popular because of 3 reasons: (i) easier to create, (ii) preferred export and import
format for databases and spreadsheets, (iii) capable of storing large amount of data.

You might also like