This document provides an introduction to file handling in Python. It discusses different types of files like text files, binary files, and CSV files. It explains how to open, read, and write to files in various modes. It also covers pickling/unpickling for serialization and deserialization of Python objects to binary streams. Key file methods like open(), read(), readline(), readlines(), write(), and writelines() are described along with examples of working with CSV files using the csv module.
2. INTRODUTION
A file in itself is bunch of byes stored on some
storage device like hard –drive etc .
Files help in storing information permanently .
3. TEXT FILES
• A text file store information in ASC11 or unicode characters
• line of text is terminated,(delimited )with a special character
known EOL (end of line)
• Default mode
• Extn of text file is .txt
• Translation occur
5. Opening position of file pointer
File modes Opening position of file pointer
r , rb , r+ , rb+ , r+b Beginning of file
W, wb, w+, wb+, w+b Beginning of file
a+, ab , a+ , ab+, a+b End of file if file exist otherwise
beginning of file
6. Opening files
<file _object name> =open (<file name >)
<file_ object name> =open(<file name >,<mode>)
with open <file_name > as <file_object
with open (‘a.txt’ , ‘r’ ) as myfile
myfile = open (‘a.txt’, ‘ r’ )
7. READING FILES
METHOD SYNTAX DESCRIPTION
Read() <filehandle>. read([n]) Read at most n bytes; if no n is
specified , reads the entire file ,
return form of string
Readline() <filehandle>.readline([n]) Reads a line of input , if n is
specified reads at most n bytes,
returns in form of string
Readlines() <filehandle> . readlines() Read all lines and return in a list
8. Writing onto files
Method Syntax Description
write () <file handle>.write(str1) writes string str1 to file
referenced by <file
handle>
writelines() <file
handle>.writeline(L)
write all strings in list L as
lines to file referenced by
<filehandle>
9. Standard Input ,Output and error stream
• Standard input device( stdin ) – reads from the
keyboard
• Standard output device (stdout) – prints to the
display and can redirected as standard input
• Standard error device(stderr) – same as stdout but
normally only for errors
10. Binary files
A binary file is just a file a that contain information in the
same format in which the information is held in memory
There is no delimiter for a line
No translations occur required in binary files
Faster and easier for a program to read and write
Extn is .dat file
11. Pickling and unpickling
• pickling refers to the process of converting the
structure(such as list or dict ) to a byte stream before
writing to the file .
Structure(list or
dictionary)
Pickling Byte stream
12. pickle.dump ()
Used to write object into file
syntax : pickle.dump(<structure>,fileobject)
file object is the file handle of the file where you write
the contents
16. CSV Files
• CSV files are comma seperated values , each line
of the file is data record .Each record consist of
one or more fields seperated by commas .Data
stores in the form of rows and columns
• It is a text file
• It is mainly useful in importing and exporting
excel files into csv files and processing in python
• File object , csv object to be created
• Accessing is fast
• Can store large amount of data
• Extn is .csv
17. Reader () - read from the file
Syntax
csv.reader (file , delimiter)
Default delimiter comma(,)
Delimiter can be of colon( : ),semicolon (:),tab (t), pipe (|))
Writer() – write into the file
syntax
csv.writer(file)