File Handling in Python
File Handling in Python
Data
Files
The text files can be opened in The binary files are not in
4. any text editor and are in human readable form.
human readable form.
Working With Data Files
‘w+b’ or write and If the file does not exist, file is created.
‘w+’ If file exist, file is truncated.
wb+ read Both reading and writing operations can take place.
e.g. myfile=open(“student.txt”)
A file-object is also known as file-handle, is a reference to a
file on disk. It opens it & makes it available for different tasks.
[Python will look this file in current working directory (Directory
in which, we store our program or module file)]
Opened file is attach to its file object e.g. myfile (file object)
Default mode of opened files is read mode (or we may assign
mode as “r” for read mode)
NOTE: In read mode, the given file must exist in the folder,
otherwise Python will raise FileNotFound Error.
Opening and Closing Files
<file_objectname>=open(<filename>, <mode>)
myfile=open(“student.txt”, “r”)
myfile1=open(“student.txt”, “w”)
myfile2=open(“e:\\main\\student.txt”, “w”)
Path : Python will look in E: drive\main folder
File objects are used to read and write data to a file on disk.
The file object is used to obtain a reference to the file on
disk and open it for a number of different tasks.
All the functions we perform on a data file are
performed through file-objects.
File mode governs the type of operations
(e.g. read/write/append) possible in the opened file i.e. it
refers to how the file will be used once it’s opened?
close() method is used to close a file. In Python, files
are automatically closes at the end of the program but it is
good practice to close files explicitly. Because if program
exits unexpectedly there is a danger that data may not have
been written to the file!
Opening and Closing Files
file3.close()
Code Snippet 2
Output
Output
Reading a file entire content.
Output
Reading a file’s first three lines- line by line.
Output
Reading a complete file – line by line.
Output
Displaying the size of a file after removing EOL (\n)
characters, leading and trailing white spaces and blank lines.
Text File : ssc.txt Code Snippet
Output
Reading a complete file in a List.
Output
Write a program to display the number of lines in the file.
Output
Working with Text Files
Writesstring str to
1. write() <fileobject>.write (str) file referred by <fileobject>.
Code Snippet
Output
Create a file to hold data of 5 names separated as lines.
Code Snippet
Output
Creating a file with some names separated by newline
characters without using write() function.
Code Snippet
Output
The flush() Function
<fileobject>.flush()
Write a program to get roll numbers, names and marks of
the students of a class (prompt user) and store these details
in a file called “Marks.txt”.
Code Snippet
CSV files are delimited files that store tabular data (data
stored in rows and columns).
The separator character of CSV files is called a delimiter.
Default and most popular delimiter is comma.
Other are tab (\t), colon (:), pipe (|) and semi-colon
(;) characters.
Since CSV files are text files, we may apply text file
procedures on these and then split values using split() function,
but using csv module in Python we may handle CSV files.
The csv module of Python provides functionality to read
and write tabular data in CSV format.
Two specific types of objects – the reader and writer objects
to read and write into CSV files.
Why CSV files are popular?
Easier to create.
Preferred export and import for databases and
spreadsheets.
format
Capable of storing large amounts of data.
CSV file opened in write mode with the file handle as obj
CSV file is closed
in the same
fobj=open(“student.csv”, “r”) manner as any
other file.
CSV file opened in read mode with the file handle as fobj
Writing in CSV files.
MEMORY
csv.writerow() is
used to write
onto the writer
object
csv.writer object
It converts the user data Delimited Data
csv.writerow()
into csv writable form, i.e.
Input User Data
delimited string form as
per csv settings.
CSV File on
ROLE OF THE CSV WRITER OBJECT
storage disk
FUNCTIONS
csv.writer() returns a writer object which writes data into CSV file
Structure
Byte Stream
(List/ Dictionary) Unpickling / De-Serialisation
Program Code
Program Code
Output
Reading and Writing into Binary File (Structure : Dictionary)
Program Code
Output
Reading ,Writing (Multiple Records) & Searching into Binary
File (Structure : Nested List)
Continue…..
Reading ,Writing (Multiple Records) & Searching into Binary
File (Structure : Nested List)
Continue…..
Reading ,Writing (Multiple Records) & Searching into Binary
File (Structure : Nested List)
Output
Setting Offsets in a File
Output
Program to know the Position of your File Pointer
Output
Program to know the Position of your File Pointer
Output
Program to know the Position of your File Pointer
Output
Program to know the Position of your File Pointer
Output
Program to Position your File Pointer at Specified Location in
File
Text File : ssc.txt Code Snippet
1
2
Output
1By default read mode keep
the file pointer in the starting of
the file.
2Default value of
from_what/ reference point is
also ‘0’, which also keeps the
file pointer in the starting of the
File.
Difference between read() and seek()
Output
Program to Position your File Pointer at Specified Location in
File
Text File : ssc.txt Code Snippet
Output
Program to Position your File Pointer at Specified Location in
File
Text File : ssc.txt Code Snippet
Output
Output
Program to Position your File Pointer at Specified Location in
File
Text File : ssc.txt Code Snippet
Output
Program to Position your File Pointer at Specified Location in
File
Text File : ssc.txt Code Snippet
Output
Program to Position your File Pointer at Specified Location in
File
Text File : ssc.txt Code Snippet
Output