0% found this document useful (0 votes)
8 views21 pages

File Handling Latest

The document provides an overview of file handling in Python, detailing types of data files such as text, binary, and CSV files, as well as common operations like creation, reading, writing, and closing files. It explains the use of the open() and close() functions, along with methods for reading and writing data, including serialization and deserialization for binary files. Additionally, it highlights the importance of the io module and the use of the with clause for automatic file closure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

File Handling Latest

The document provides an overview of file handling in Python, detailing types of data files such as text, binary, and CSV files, as well as common operations like creation, reading, writing, and closing files. It explains the use of the open() and close() functions, along with methods for reading and writing data, including serialization and deserialization for binary files. Additionally, it highlights the importance of the io module and the use of the with clause for automatic file closure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

File Handling

● File: Named location on a secondary storage media where data


are permanently stored for later access.
Data can be represented in specific forms.
● File handling: Mechanism by which data is handled by software
including I/O operations.

★ Read data of disk files in


Python program
★ Write back data from
Python program to disk
files
Types of Data Files
● Text File: Consist of human readable characters
Can be opened by any text editor
Stored in sequence of bytes(1’s & 0’s)
Each line terminated by End of Line(EOL) character [\n or \r]
● Binary File: Stored as 0’s & 1’s [consist of information in the same format how its
stored in memory]
Represent the actual content like image, audio, video, compressed files etc.,

● CSV File: (Comma Separated Values) is a file format - looks like a text file. The
information is organized with one record on each line and each field is separated
by comma.
Data File Operations:

● Creation of files
● Opening files
● Reading files
● Writing files
● Appending data in files
● Deleting data in files
● Creating copy
● Updating data in file
❖ io module consist of built-in functions to perform all operations
❖ Before performing any function, you need to open the file in the specific mode
Opening a file: open() function
Syntax: file_object=open(file_name,access_mode)
● file_object: variable for storing file object returned by the open()
Establish a link between the program & the file
Used for read & write operation in that file
● file_name : name of the file(if the file is in the same directory) or specify the path of the file
● access_mode: governs the type of operation possible in the opened file
By default: read mode

Eg: myfile=open(“data.txt”,”a”)
Closing a file: close() function

Syntax: file_object.close()
● Save unsaved data in to the file
● It will break the linkage of file from the program
● No more operations can be performed on the file using the file object

Opening a file using with clause:

Syntax: with open(“file_name”,”access_mode”)as file_object:


Any file opened using with clause will be closed
automatically
Writing to a text file: write() & writelines() functions
● Open in write or append mode.
If we open an existing file in write mode, the previous data will get erased
& the file object will point to the beginning of the file
If we open an existing file in append mode, new data will be added at the
end of the previous data
❖ write() : for writing a single string
It takes a string as a parameter
❖ writelines() : for writing a sequence of strings [as lines, strings, tuples etc.,]
User input – writelines( )

User input – write( )


Reading from file : read(), readline(), readlines() function

● read(): To read the whole file


● read(n): To read ‘n’ number of characters.

If the file contain less than ‘n’ characters, then it will read up to
EOF.

● readline(): To read a line from the file


● readlines(): To read specific number of lines from a file
Working With Binary Files

● Serialisation/ Pickling: Process of converting Python object into a byte


stream to write into a file.
● Deserialisation/ Unpickling: Process of converting the byte stream into
object hierarchy.
○ Python provides pickle module to achieve this.
import pickle
○ dump() : To write data into binary file.
■ pickle.dump(<object_to_be_written>,<file_object>)
○ load() : To read data from binary file.
■ <object>=pickle.load(<file_object>)
emp.append(pickle.load(f))
Searching
in Binary
Files
CSV FILES
Writing data in CSV file
Reading data from CSV file

You might also like