0% found this document useful (0 votes)
3 views

Text File

Uploaded by

golaharshit53
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Text File

Uploaded by

golaharshit53
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Text file

A text file can be understood as a sequence of charactersconsisting of alphabets, numbers


and other special symbols. Files with extensions like .txt, .py, .csv, etc. are some
examples of text files.
However, the file contents are not stored in such a way internally. Rather, they are
stored in 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.Contents
in a text file are usually separated by whitespace, but comma (,) and tab (\t) are also
commonly used to separate values in a text file.

Binary Files
Binary files are also stored in terms of bytes (0s and 1s), but unlike text files, these
bytes do not represent the ASCII values of characters. Rather, they represent the actual
content such as image, audio, video, compressed versions of other files, executable
files, etc. 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.

.1 The tell() method


This function returns an integer that specifies the current position of the file object
in the file. The position so specified is the byte position from the beginning of the
file till the current position of the file object. The syntax of using tell() is:
file_object.tell()
.2 The seek() method
This method is used to position the file object at a particular position in a file. The
syntax of seek() is: file_object.seek(offset [, reference_point]) In the above syntax,
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

Pickle module
Python pickle module is used for serializing and de-serializing a Python object
structure. Any object in Python can be pickled so that it can be saved on disk. What
Pickle does is it “serializes” the object first before writing it to a file. Pickling is
a way to convert a Python object (list, dictionary, etc.) into a character stream. The
idea is that this character stream contains all the information necessary to reconstruct
the object in another Python script. It provides a facility to convert any Python object
to a byte stream. This Byte stream contains all essential information about the object so
that it can be reconstructed, or “unpickled” and get back into its original form in any
Python.

The dump() method


This method 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) where data_object is
the object that has to be dumped to the file with the file handle named file_object.

The load() method


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)

• write() method takes a string as an argument and writes it to the text file.
• writelines() method is used to write multiple strings to a file. We need to pass an
iterable object like lists, tuple etc. containing strings to writelines() method.
• read([n]) method is used to read a specified number of bytes (n) of data from a data
file.
• readline([n]) method reads one complete line from a file where lines are ending with a
newline
(\n). 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).
• readlines() method reads all the lines and returns the lines along with newline
character, as a list of strings.

You might also like