UNIT -III_Python file handling, reading and writing files.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
FILE HANDLING Introduction Files are named locations on a disk to store information permanently. Programmers need to save and load information to and from files. Introduction...
FILE HANDLING Introduction Files are named locations on a disk to store information permanently. Programmers need to save and load information to and from files. Introduction To work with files in Python, it is essential to load them into File Loading in memory. Python: Python interacts with files through "file handles." File handles are interfaces that enable Python to communicate with files in memory. File Handles: When a file is opened, the operating system provides Python with a file handle. The operating system locates the file in memory when opened Operating System by Python. Interaction: If found, the operating system returns a file handle to Python. A file handle functions like a cursor for file operations. File Handle Analogous to Cursor: It determines the position from which data should be read or written in the file. Introduction The basic file operations are open a file read or write or append data close a file Types of File ◦ Text Files: ◦ Binary Files: 1.Store character data, making them suitable 1.Store data in the form of bytes, for storing strings. accommodating various types like text, 2.Each line in a text file ends with the "End of images, audio, and video. Line" (EOL) character, typically '\n' in 2.No line terminators; data is stored in Python. machine-understandable binary language. 3.Human-readable and editable due to the 3.Fast to save and load, and highly compact. plain text format. 4.Not human-readable or editable, optimized for performance. 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]) File name – Name of the file to be opened In case if file location is else where, we must need to provide the exact path. Access_mode -Access modes determine the operations that can be performed on an opened file. access mode is optional, in case of not mentioning it will be default (r) File Attributes Closing a file ◦ After completing file activities, the file must be closed. Opening the files could lead to resource waste, data loss, file corruption, and inconsistencies. The opened file is closed using Python's built-in close() method. file_object.close() Example: file = open(“test.txt”) file.close() FILE METHODS 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. used in serializing and deserializing a Python object structure. 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