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

File Handling

This document discusses different types of files and file handling in Python. It covers text files, binary files, CSV files, and the differences between them. It also describes how to open, read, write, append, and close files, and covers common file operations like seek(), tell(), and using stdin, stdout, and stderr.

Uploaded by

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

File Handling

This document discusses different types of files and file handling in Python. It covers text files, binary files, CSV files, and the differences between them. It also describes how to open, read, write, append, and close files, and covers common file operations like seek(), tell(), and using stdin, stdout, and stderr.

Uploaded by

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

FILE HANDLING

The data files store data pertaining to some applications.


1. There are two kinds of files
1. Text files
2. Binary files
3. csv files

4. The difference between text file and binary files

Text file Binary file


Text files store information in Binary files store data in the
the ASCII Format format in which ever way it
is stored in memory
Text files each line is No EOL CHARACTER
terminated with special EOL
character
Special translations take No such translations take
place when it encounters place as there is no EOL
special EOL character
To fetch content from file it Writing to file and reading
takes more time .query from files are faster. Query
processing is slower because processing is faster becuase
data is stored in ASCII the data is stored in
FORMAT machine readable format.
The extension of the text file The extension of thebinary
is .txt file is .dat
5. The basic file manipulations are reading , writing and
appending contents to file.
6. open() function is used to open the file.
7. close() function is used to close the file.
8. The format for opening and closing files
F=open(“c:\\python\\file\\series.txt”,”w”)
8. To close the file
F.close()
9. the second parameter in the open function is meant for
mode of the file.
TEXT FILE BINARY FILE
r=read mode rb=read mode
w=write mode wb=write mode
a=append mode ab=append mode
r+ both read and write mode rb+ both read and write mode
w+both read and write mode wb+both read and write mode
a+ both read and append ab+ both read and append
mode mode
“r+b ,” w+b” “a+b”

10. r+, rb+(r+b) modes will enable the user to read and also
write contents to file if file is already created. If file is not
created then it will not be able create the file
11. w+, wb+(w+b) will enable the user to read and write
contents. If file does not exist it will create a file and also can
read contents from file
12. a+, ab+(a+b) will enable the user to append contents to file
if file is not existing it will create and write and also user can
read content from the file.
The same rules are applicable to binary also.
For reading and writing the text file
13. For writing contents to files we have 2 functions
Write (st) for writing the string
Writelines(list) takes list as a parameter

14. For reading the content from file


read()—reads entire file
read(n)—reads ‘ n’ number of bytes
readlines()-reads entire file
readline([n]))—reads only one line
15. In case of binary file, the data can be written to file using
following function.
pickle.dump(l,fw) where l is list and fw is file stream object
16. pickle package should be imported to the program where
binary file is created.
17. content from the file can be read in the following manner:
r=pickle.load(f) where f is file stream object.
18. the end of the file error can be trapped in the following
manner by using try and except EOFError.

while True:
try:
r=pickle.load(f)
print(r)
except EOFError:
break
19. file can be opened in the following manner using with block.
This will automatically close the file . it does not require to
explicitly closing of the file.
with open("cloth.dat","rb")as f:
while True:
try:
r=pickle.load(f)
print(r)
except EOFError:
break

20. CSV comma separated file. The extension of the file is


.csv . the opening and closing of the csv file is done in the
following manner.
21 . csv file can be opened in the following manner for the
purpose of writing records to the file.
Fn=”IT.csv”
File name can be assigned to a variable name. this rule is
applicable to all kinds of files.csv.writer() is used for writing
contents to csv file.
csvfilew = open(fn,'w')
csvw=csv.writer(csvfilew,lineterminator='\n')
22. when you print csvw it prints the address to where the
content is fectched.
23. the record which is a list can be written using write row
function
csvw.writerow(item)
24. writerows() function is used to write a list which contains
more than one record.
25. csv file can be opened in the following manner for the
purpose of reading records from the file. Csv.reader(file
object)
with open(fn,'r')as csvfiler:
csvr=csv.reader(csvfiler)
for r in csvr:
if len(r)>0:
print(r)
26. CSV files can be opened in excel application.
27. path:-the entire location of the file
C:\python\progrmas\content.txt

Absolute path:- the path from the root drive to the present
directory in which the required file is existing.
Relative path:-This path is relative to the current working
directory.
. is for current working directory
..\-one step back.

We have to import sys package for using sys.stdin.readline(),


sys.stdout.write(). Sys.stderr.write()---
their implementation is shown in the following program

stdin—is connected to keyboard


stdout &stderr is connected ---monitor

in the place of input sys.stdin.readline() can be used


in the place of print—sys.stdout.write() can be used
sys.stderr.write can be used to print error after trapping which
will be printed in red colour.
Sys.stdin.readline()_
f=open("series.txt","w")
#st=input("accept the string")
print("accept the string")
st=sys.stdin.readline()
f.write(st)
f.close()
"""
f=open("series.txt")
f.seek(10)
st=f.read()
print("using print")
print(st)
print("using sys.stdout.write")
sys.stdout.write(st)
print("this is an error")
sys.stderr.write("this is an error")

seek(n) function is used to bring the pointer to the required


location which is passed as a parameter.
“n” stands number of bytes.

tell() functions returns no of bytes read.


C=f.tell()

You might also like