UNIT -III_Python file handling, reading and writing files
UNIT -III_Python file handling, reading and writing files
Introduction
Considerations:
1.Text files are more user-friendly, while binary files are chosen for performance reasons.
2.Text files are readable and editable by humans, whereas binary files are not.
File Operations
◦ Opening file
1st step is to open a file, in python we use built in function open() function.
file_object = open(file_name [, access_mode])
write()
writelines()
read()
readline()
readlines().
Writing to a file - write()
◦ Any string can be written to an open file using the write() method. Python
strings are capable of holding both text and binary data.
◦ file_object.write(str1)
Writing to a file (Multiple Lines)- writelines
◦ The writelines() method writes multiple strings at a single time in the text file.
Reading from a file
◦ The read() methods returns the read bytes in the form of a string. While
reading the file, we can specify the number of bytes to read as n bytes, if n
is not specified, the entire content of the file will be returned
Example:-
my_file = open(“sample.txt”, “r”)
print(my_file.read());
my_file.close()
File Built In Methods
◦ tell () Method
Tell method is used to find out the current position of pointer in file.
◦ Rename() Method
The os module in Python provides a way of interacting with the operating system.
os module provides methods that help you perform file processing operations,such as
renaming files and deleting files.
rename()
os.rename(old_Name,New_Name);
File Built In Methods
◦ Remove()
The remove() method is used to remove any existing file.
os.remove(File_name)
Pickle Module
Usually, data will be converted to bytes before converting into binary files.
Python pickle module can be used to convert a Python object (list, dict, etc.) into a character stream.
The idea is that this character stream contains all the information necessary to reconstruct the object in another
python script.
The process that converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) to store it in a
file/database is called pickling or serialization or flattening.
Byte stream can be Converted back into python objects which is called as unpickling or de-serializing.
The pickling and unpickling allow us to easily transfer data from one
server/system to another and then store it in a file or database.
Pickle Module
◦ Pickle module needs to be imported to perform serialization or de-serialization
import pickle
Methods in pickle module
• pickle.dump() • pickle.load()
Unpickling
• pickle.dumps() • pickle.loads()
pickling
READING & W RITING CSV FILES
CSV (Comma Separated Values) format is the most common import and export format
for spreadsheets and databases.
It is one of the most common methods for exchanging data between applications and
popular data format used in Data Science.
A CSV file stores tabular data in which each data field is usually separated by a delimiter
comma.
To represent a CSV file, it must be saved with the CSV file extension. The CSV library
provides functionality to both read from and write to CSV files.
IMPORTING CSV
◦ We need to import csv module in order to work with CSV files in python
import csv
After importing csv file needs to be opened – open() method
The file object should be opened with newline=” otherwise, newline
characters inside the quoted fields will not be interpreted correctly.
Writing and Read to CSV file
◦ To write a CSV FILE csv.writer() function after opening the file in write mode.
◦ To read a CSV file in Python, we can use the csv.reader() function
after opening the file in read mode.
Reading and Writing to a CSV file using pandas
◦ Pandas is an open-source Python library that provides high performance data analysis tools and easy
to use data structures. Pandas is a popular data science library in Python for data manipulation and
analysis.
◦ Import pandas library
import pandas as pd