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

Computer Science Chapter

The document discusses data files in Python. It explains that data files store application data and can be text files or binary files. Text files use characters and delimit lines while binary files have no delimiters. The document also covers opening, reading from, and writing to files in Python using functions like open(), read(), readline(), readlines(), write(), and writelines(). It provides the syntax and usage of these functions to manipulate files.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Computer Science Chapter

The document discusses data files in Python. It explains that data files store application data and can be text files or binary files. Text files use characters and delimit lines while binary files have no delimiters. The document also covers opening, reading from, and writing to files in Python using functions like open(), read(), readline(), readlines(), write(), and writelines(). It provides the syntax and usage of these functions to manipulate files.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Data File

The data files are the files that store data pertaining to a specific
application. It is a bunch of files store on some storage devices. Like hard
disk, flash device, CD, DVD etc. The data files can be stored in two ways.
1) Text File 2) Binary Files

1) Text File :- A text file stores information in ASCII or Unicode


characters. In text files each line of text is terminated (delimited) with a
special character known as EOL (End Of Line). In Python by default this
EOL character is new line character (\n) or carriage return (\r) or
combination of both (\n\r)
2) Binary File :- In binary file there is no delimiter for a line. Binary files are faster
and easier for program to read and write than text file.

Opening and Closing Files :- In Python you need to open a file in a specific mode as
per the file manipulation task you want to perform. The most base task for
manipulation of files are adding, deleting or modifying data in a file. Which in turn
include any one or combination of following.

* Reading data from file


* Writing data to file
* Appending data to file

Opening File :- In data file handling Python, the first you need to open file using
open() function.
Syntax :- File objects are
<file_object_name>=open(<file_name>) used to read
<file_object_name>=open(<file_name>,<mode>) and write data
to file on disk.
Example
myFile1 = open(‘taxes.txt’)

File object Function File name Mode


With r here
myFile2 = open(‘taxes.txt’,’r’) you can give
single slash in
path name or
myFile3 = open(‘E:\\main\\result.txt’,’r’) otherwise double
myFile4 = open(r ‘E:\main\result.txt’,’w’) slash given.
File Access Mode
Text File Binary File Description Note
Mode Mode
‘r’ ‘rb’ Read only File must be exist already otherwise
Python raise I/O error
‘w’ ‘wb’ Write only *If the file does not exist file is created.
*If file is exists Python will truncate
existing data and over write in the file. So
this mode must be used with caution.
‘a’ ‘ab’ append *File is in write mode only.
*If the file exists the data in the file is
retained and new data being written will
be appended to the end of file.
*If file does not exists Python will create a
new file
File Access Mode
Text File Binary File Description Note
Mode Mode
‘r+’ ‘r+b’ or Read and *File must be exist already otherwise
‘rb+’ Write Python raise I/O error.
* Both read and write operations.
‘w+’ ‘w+b’ or Write and *If the file does not exist file is created.
‘wb+’ Read *If file is exists Python will truncate
existing data and over write in the file.
* Both read and write operations.
‘a+’ ‘a+b’ or Write and *If file does not exists Python will create a
‘ab+’ Read new file.
*If the file exists the data in the file is
retained and new data will be appended to
the end of file.
Closing Files:- An open file is close by calling the close() method of
its file object. Closing of file is important. In Python files are
automatically closed at the end of the program. But it is good
practice to close the file yourself.

Syntax:-
<file handle>.close()
<file object>.close()
myFile.close()

Reading and Writing:- Python provide many functions for reading and
writing the open files
Reading From File
Sno Method Syntax Description
1 read() <fo>.read([n]) *read n byte if n is specified otherwise
read the entire file.
*Return the read byte in the form of a
string.

fileobj=object.open(‘info.txt’,’r’)
info=fileobj.read(15) Reads 15
print(info) characters from
info.txt file in
the form of
string.
Reading From File
Sno Method Syntax Description
2 readline() <fo>.readline([n]) *Reads a line of input; if n is specified
reads at most n bytes.
*Return the read byte in the form of
string ending with \n character of
return a blank string if no more bytes
are left for reading in the file.

fileobj=object.open(‘info.txt’,’r’)
info=fileobj.readline() Read one line
print(info)
info=fileobj.readline(5) Read 5 bytes
print(info)
Reading From File
Sno Method Syntax Description
3 readlines() <fo>.readlines() *Reads all lines in the form of list. Line 1
store index 0 and line 2 index 1 and so
on.

fileobj=object.open(‘info.txt’,’r’)
info=fileobj.readlines()
print(info)
Read all lines
Info is list type
Reading From File

Output
Reading From File

Output
Reading From File

Output
Reading From File

Output
Reading From File

Output
Reading From File

Output
Writing into File
Sno Method Syntax Description
1 write() <fo>.write(st) *Write string st into the file
representing by <file handler>.
2 writelines() <fo>.writelines(L) *Write all string in list L as line into
the file representing by
<file handler> .
File object File name File Mode

file=open(‘record.txt’,’w’) File opening


file.write(‘Hello how are you?’)
file.close()
File closing
Writing into File
Writing into File
The flush() function:- Python holds everything to write in the file in a buffer
and pushes it into actual file on storage device a later time. You want to
force Python to write the content of buffer on to storage you can use flush
function. Python automatically flushes the file when closing file.

You might also like