COMPUTER SCIENCE WITH PYTHON CBSE New Syllabus(Session-2023-24) CHAPTER NO – 3 File handling Unit-1 (Computational Thinking and Programming-2) Need for a data file Need for a data file :- Need for a data file :• To Store data in organized manner • To store data permanently • To access data faster • To Search data faster • To easily modify data later on Unit-1 (Computational Thinking and Programming-2) File Handling File: A file is a sequence of bytes on the disk/permanent storage where a group of related data is stored. File is created for permanent storage of data. File handling: in Python enables us to create, update, read, and delete the files stored on the file system through our python program. The following operations can be performed on a file. In programming, Sometimes, it is not enough to only display the data on the console. Those data are to be retrieved later on, then the concept of file handling comes. It is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the file system which is not volatile and can be accessed every time. Here, comes the need of file handling in Python. Unit-1 (Computational Thinking and Programming-2) File Handling : - Types of File In Python, File Handling consists of following three steps: Open the file & Process file perform read or write operation, Close the file. There are three types of files: Text Files :- A file whose contents can be viewed using a text editor is called a text file. A text file is simply a sequence of ASCII or Unicode characters. Python programs, contents written in text editors are some of the example of text files.e.g. .txt,.rtf,.csv etc. Binary Files :- A binary file stores the data in the same way as as stored in the memory. The .exe files,mp3 file, image files, word documents are some of the examples of binary files. we can’t read a binary file using a text editor.e.g. .bmp,.cdr etc. CSV Files:- A Comma Separated Values (CSV) file is a plain text file that contains a list of data. These files are often used for exchanging data between different applications. For example, databases and contact managers often support CSV files. Unit-1 (Computational Thinking and Programming-2) File Handling : - Types of File File Handling : Types of File Text File Its Bits represent character. Binary File Its Bits represent a custom data. Less prone to get corrupt as change reflects as soon as made and can be undone. Can easily get corrupted, corrupt on even single bit change Store only plain text in a file. Widely used file format and can be opened in any text editor. Mostly .txt,.rtf are used as extensions to text files. Can store different types of data (audio, text,image) in a single file. Developed for an application and can be opened in that application only. Can have any application defined extension. Unit-1 (Computational Thinking and Programming-2) File Handling : - Opening and Closing Files File Handling : Opening and Closing Files Opening and Closing Files :To perform file operation ,it must be opened first then after reading,writing, editing operation can be performed. To create any new file then too it must be opened. On opening of any file ,a file relevant structure is created in memory as well as memory space is created to store contents. Once we are done working with the file, we should close the file. Closing a file releases valuable system resources. In case we forgot to close the file, Python automatically close the file when program ends or file object is no longer referenced in the program. However, if our program is large and we are reading or writing multiple files that can take significant amount of resource on the system. If we keep opening new files carelessly, we could run out of resources. So be a good programmer , close the file as soon as all task are done with it. Unit-1 (Computational Thinking and Programming-2) File Handling : - Open Function File Handling : - Open Function Before any reading or writing operation of any file, it must be opened first of all. Python provide built in function open() for it. On calling of this function creates file object for file operations. Open Function :Syntax file object = open(<file_name>, <access_mode>, < buffering>)file_name = name of the file ,enclosed in double quotes. access_mode= Determines the what kind of operations can be performed with file, like read, write etc. Buffering = for no buffering set it to 0. For line buffering set it to 1. If it is greater than 1, then it is buffer size. If it is negative then buffer size is system default. Unit-1 (Computational Thinking and Programming-2) File Handling :Opening & closing files File Handling : Opening & closing files Unit-1 (Computational Thinking and Programming-2) File Handling : - Open Function File Handling : access modes S.No. Mode & Description 1 r - reading only.Sets file pointer at beginning of the file . This is the default mode. 2 rb – same as r mode but with binary file 3 r+ - both reading and writing. The file pointer placed at the beginning of the file. 4 rb+ - same as r+ mode but with binary file 5 w - writing only. Overwrites the file if the file exists. If not, creates a new file for writing. 6 wb – same as w mode but with binary file. 7 w+ - both writing and reading. Overwrites . If no file exist, creates a new file for R & W. 8 wb+ - same as w+ mode but with binary file. 9 a -for appending. Move file pointer at end of the file.Creates new file for writing,if not exist. 10 ab – same as a but with binary file. 11 a+ - for both appending and reading. Move file pointer at end. If the file does not exist, it creates a new file for reading and writing. 12 ab+ - same as a+ mode but with binary mode. Unit-1 (Computational Thinking and Programming-2) File Handling : - Basic Text file operations File Handling : Basic Text file operations • Open (filename – absolute or relative path, mode) • Close a text file • Reading / Writing data • Manipulation of data • Appending data into a text file Unit-1 (Computational Thinking and Programming-2) File Handling: Reading a file File Handling : Reading a file Unit-1 (Computational Thinking and Programming-2) File Handling: Reading a file File Handling : Reading a file Unit-1 (Computational Thinking and Programming-2) File Handling :writing to a file File Handling : Writing to a file Unit-1 (Computational Thinking and Programming-2) File Handling :writing to a file File Handling : Writing to a file Unit-1 (Computational Thinking and Programming-2) File Handling :writing to a file File Handling : Writing to a file Unit-1 (Computational Thinking and Programming-2) File Handling : Appending in a file File Handling : Appending in a file Unit-1 (Computational Thinking and Programming-2) File Handling : -writing user input to the file File Handling : writing user input to the file Unit-1 (Computational Thinking and Programming-2) File Handling : - Basic Text file operations File Handling : Basic Text file operations e.g. program import os print(os.getcwd()) os.mkdir("newdir") os.chdir("newdir") print(os.getcwd()) Methods of os module Before Starting file operation following methods must be learn by a programmer to perform file system related methods available in os module(standard module) which can be used during file operations. The rename() method used to rename the file. Syntax: os.rename(current_file_name,new_file_name) The remove() method to delete file. Syntax : os.remove(file_name) The mkdir() method of the os module to create directories in the current directory. Syntax: os.mkdir("newdir") Unit-1 (Computational Thinking and Programming-2) File Handling : - Basic Text file operations File Handling : Basic Text file operations The chdir() method to change the current directory. Syntax os.chdir("newdir") The getcwd() method displays the current directory. Syntax os.getcwd() The rmdir() method deletes the directory. Syntax os.rmdir('dirname') Unit-1 (Computational Thinking and Programming-2) File Handling : - Absolute Path vs Relative Path File Handling : Absolute Path vs Relative Path Absolute Path vs Relative Path :One must be familiar with absolute & relative path before starting file related operations. The absolute path is the full path to some place on your computer. The relative path is the path to some file with respect to your current working directory (CWD). For example: Absolute path: C:/users/admin/docs/staff.txt If CWD is C:/users/admin/, then the relative path to staff.txt would be: docs/staff.txt Note, CWD + relative path = absolute path. Unit-1 (Computational Thinking and Programming-2) File Handling : - Absolute Path vs Relative Path e.g. program File Handling : Absolute Path vs Relative Path import os print(os.getcwd()) os.mkdir("newdir1") os.chdir("newdir1") print(os.getcwd()) my_absolute_dirpath=os.path.abspath(os.path.dirname ( file )) print(my_absolute_dirpath) Unit-1 (Computational Thinking and Programming-2) File Handling : - File object attributes File Handling : File object attributes demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! open a text file closed: It returns true if the file is closed and false when the file is open. To open the file, use the built-in open() function. The open() function returns a file object, which has a read() method for reading the content of the file: encoding: Encoding used for byte string conversion. mode: Returns file opening mode name: Returns the name of the file which file object holds. newlines: Returns “\r”, “\n”, “\r\n”, None or a tuple containing all the newline types seen. Output:e.g. Program f = open("demofile.txt", "r") Hello! Welcome to demofile.txt print(f.read()) This file is for testing purposes. Good Luck! Unit-1 (Computational Thinking and Programming-2) File Handling : - File object attributes File Handling : File object attributes demofile.txt Hello! Welcome todemofile.txt This file is for testing purposes. Good Luck! open a text file If the file is located in a different location, you will have to specify the file path, like this: f = open("D:\\myfiles\welcome.txt", "r") print(f.read()) Output:f = open("D:\\myfiles\welcome.txt", "r")print(f.read()) Welcome to this text file! This file is located in a folder named "myfiles", on the D drive. Good Luck! Unit-1 (Computational Thinking and Programming-2) File Handling : - File object attributes open a text file File Handling : File object attributes demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! readline() Read one line of the file: e.g. program f = open("demofile.txt", "r") Output:print(f.readline()) Hello! Welcome to demofile.txt Read two lines of the file: e.g. program Output:f = open("demofile.txt", "r") Hello! Welcome to demofile.txt print(f.readline()) This file is for testing purposes. print(f.readline()) Unit-1 (Computational Thinking and Programming-2) File Handling : - File object attributes open a text file File Handling : File object attributes demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! Loop through the file line by line: e.g. program f = open("demofile.txt", "r") for x in f: print(x) Output:Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! Unit-1 (Computational Thinking and Programming-2) File Handling : - File object attributes File Handling : File object attributes demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! open a text file Read Only Parts of the File By default the read() method returns the whole text, but you can also specify how many characters you want to return: Return the 5 first characters of the file: e.g. program f = open("demofile.txt", "r") print(f.read(5)) Output:- Hello Unit-1 (Computational Thinking and Programming-2) File Handling : - Close a text file File Handling : File object attributes demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! Close a text file :close(): Used to close an open file. After using this method, an opened file will be closed and a closed file cannot be read or written any more. Close the file when you are finish with it: e.g. program f = open("demofile.txt", "r") print(f.readline()) f.close() Output: Hello! Welcome to demofile.txt Unit-1 (Computational Thinking and Programming-2) File Handling : - Read/write text file File Handling : File object attributes demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! Read/write text file The write() Method It writes the contents to the file in the form of string. It does not return value. Due to buffering, the string may not actually show up in the file until the flush() or close() method is called. The read() Method It reads the entire file and returns it contents in the form of a string. Reads at most size bytes or less if end of file occurs. If size not mentioned then read the entire file contents. Unit-1 (Computational Thinking and Programming-2) File Handling : - Read/write text file File Handling : File object attributes Python Delete File Python Delete File Delete a File To delete a file, you must import the OS module, and run its os.remove() function: Example Remove the file "demofile.txt": import os os.remove("demofile.txt") Unit-1 (Computational Thinking and Programming-2) File Handling : - Read/write text file File Handling : File object attributes Python Delete File Python Delete File Check if File exist: To avoid getting an error, you might want to check if the file exists before you try to delete it: Example Check if file exists, then delete it: import os if os.path.exists("demofile.txt"): os.remove("demofile.txt") else: print("The file does not exist") Unit-1 (Computational Thinking and Programming-2) File Handling : - Read/write text file File Handling : File object attributes Python Delete Folder Delete Folder To delete an entire folder, use the os.rmdir() method: Example Remove the folder "myfolder": import os os.rmdir("myfolder") Note: You can only remove empty folders Unit-1 (Computational Thinking and Programming-2) File Handling : - Read/write text file File Handling : File object attributes Python Delete File Python Delete File Delete a File To delete a file, you must import the OS module, and run its os.remove() function: Example Remove the file "demofile.txt": import os os.remove("demofile.txt") Check if File exist: To avoid getting an error, you might want to check if the file exists before you try to delete it: Unit-1 (Computational Thinking and Programming-2) File Handling : - Read/write text file File Handling : File object attributes Create a New File Create a New File To create a new file in Python, use the open() method, with one of the following parameters: "x" - Create - will create a file, returns an error if the file exist "a" - Append - will create a file if the specified file does not exist "w" - Write - will create a file if the specified file does not exist Example: Create a file called "myfile.txt": f = open("myfile.txt", "x") Result: a new empty file is created! Example: Create a new file if it does not exist: f = open("myfile.txt", "w") Unit-1 (Computational Thinking and Programming-2) File Handling : - Read/write text file File Handling : File object attributes demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! write() ,read() Method based program Write to an Existing File To write to an existing file, you must add a parameter to the open() function: "a" - Append - will append to the end of the file "w" - Write - will overwrite any existing content Open the file "demofile2.txt" and append content to the file: e.g program f = open("demofile2.txt", "a") f.write("Now the file has more content!") f.close() #open and read the file after the appending: f = open("demofile2.txt", "r") Output:Hello! Welcome to demofile2.txt print(f.read()) This file is for testing purposes. Good Luck!Now the file has more content! Unit-1 (Computational Thinking and Programming-2) File Handling : - Read Text file readline([size]) method: read no of characters from file if size is mentioned till eof. read line till new line character. returns empty string on EOF. Read Text file e.g. program f = open("a.txt", 'w') line1 = 'Welcome to python' f.write(line1) line2="\nRegularly visit python" f.write(line2) f.close() f = open("a.txt", 'r') text = f.readline() print(text) OUTPUT text = f.readline() Welcome to python Regularly visit print(text) python f.close() Unit-1 (Computational Thinking and Programming-2) File Handling : - Read Text file readlines([size]) method: read no of lines from file if size is mentioned or all contents if size is not mentioned. Read Text file e.g.program f = open("a.txt", 'w') line1 = 'Welcome to python.in' f.write(line1) line2="\nRegularly visit python.in" f.write(line2) OUTPUT f.close() ['Welcome to python.in\n'] f = open("a.txt", 'r') NOTE – READ ONLY ONE text = f.readlines(1) LINE IN ABOVE PROGRAM. print(text) f.close() Unit-1 (Computational Thinking and Programming-2) File Handling : - Read Text file File Handling : read Text file Iterating over lines in a file Iterating over lines in a file e.g.program f = open("a.txt", 'w') line1 = 'Welcome to python.in' f.write(line1) line2="\nRegularly visit python.in" f.write(line2) f.close() f = open("a.txt", 'r')\ for text in f.readlines(): print(text) f.close() Unit-1 (Computational Thinking and Programming-2) File Handling : - Read Text file File Handling : read text file processing every word in a file Processing Every Word in a File f =open("a.txt", 'w') line1 = 'Welcome to python.in' f.write(line1) line2="\nRegularly visit python.in" f.write(line2) f.close() f = open("a.txt", 'r') for text in f.readlines(): for word in text.split( ): print(word) f.close() OUTPUT Welcome to python.in Regularly visit python.in Unit-1 (Computational Thinking and Programming-2) File Handling : -rename method( ) File Handling : rename method( ) rename( ) method:rename() method in Python is used to rename a file or directory. This method renames a source file/ directory to specified destination file/directory. Steps to Rename File in Python To rename a file, Please follow these steps: Find the path of a file to rename To rename a file, we need its path. The path is the location of the file on the disk. An absolute path contains the complete directory list required to locate the file. A relative path contains the current directory and then the file name. Decide a new name Save an old name and a new name in two separate variables. old_name = 'details.txt' new_name = 'new_details.txt' Unit-1 (Computational Thinking and Programming-2) File Handling : -rename method( ) Example: Renaming a file in Python In this example, we are renaming “detail.txt” to Use rename() method of an OS module “new_details.txt”. Use the os.rename() method import os to rename a file in a folder. # Absolute path of a file Pass both the old name and old_name = r"E:\demos\files\reports\details.txt" a new name to the new_name = r"E:\demos\files\reports\new_details.txt" os.rename # Renaming the file (old_name, new_name) os.rename(old_name, new_name) function to rename a file. Output: Before rename file After renaming a file Unit-1 (Computational Thinking and Programming-2) File Handling : -remove method( ) remove method( ): The remove() method takes a single element as an argument and removes it from the list. If the element doesn't exist, it throws ValueError: Example Remove the "banana" element of the fruit list: fruits = ['apple', 'banana', 'cherry'] fruits.remove("banana") Output: ['apple', 'cherry'] Unit-1 (Computational Thinking and Programming-2) File Handling : -Flush method( ) Flush method( ) : Python file method flush() flushes the internal buffer, like stdio's fflush. This may be a no-op on some file-like objects. Python automatically flushes the files when closing them. But you may want to flush the data before closing any file. !/usr/bin/python # Open a file fo = open("foo.txt", "wb") print "Name of the file: ", fo.name # Here it does nothing, but you can call it with read operation. fo.flush() # Close opend file fo.close() When we run above program, it produces following result − Name of the file: foo.txt Unit-1 (Computational Thinking and Programming-2) File Handling : - Getting & Resetting the Files Position Getting & Resetting the Files Position Getting & Resetting the Files Position: The tell() method The seek (offset [, from]) method The tell() method of python tells us the current position within the file, where as the seek (offset [, from]) method changes the current file position. If from is 0, the beginning of the file to seek. If it is set to 1, the current position is used . If it is set to 2 then the end of the file would be taken as seek position. The offset argument indicates the number of bytes to be moved. Unit-1 (Computational Thinking and Programming-2) File Handling : - Getting & Resetting the Files Position The tell() method: The tell() method returns the current file position in a file stream. demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! Return the current file position after reading the first line: e.g:program f = open("demofile.txt", "r") print(f.readline()) print(f.tell()) Output:Hello! Welcome to demofile.txt 32 Unit-1 (Computational Thinking and Programming-2) File Handling : - Getting & Resetting the Files Position tell(): In python programming, within file handling concept tell() function is used to get the actual position of file object. By file object we mean a cursor. And it’s cursor, who decides from where data has to be read or written in a file. Syntax: f.tell() #here f is file handler or file object Explanation: We opened “test.txt” file in read mode. If file opened in read mode than by default file object or file pointer at beginning ie. 0 position. Hence our output is 0. You can also see in above image (data written in text file) highlighted in yellow that pointer is at beginning. Unit-1 (Computational Thinking and Programming-2) File Handling : - Getting & Resetting the Files Position seek() method : The seek() method sets the current file position in a file stream. The seek() method also returns the new postion. demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! You can change the current file position with the seek() method. Change the current file position to 4, and return the rest of the line: e.g:program f = open("demofile.txt", "r") f.seek(4) print(f.readline()) Output:o! Welcome to demofile.txt Unit-1 (Computational Thinking and Programming-2) File Handling : - Getting & Resetting the Files Position seek()method: The seek() method sets the current file position in a file stream. The seek() method also returns the new postion. demofile.txt Hello! Welcome to demofile.txt This file is for testing purposes. Good Luck! You can change the current file position with the seek() method. Change the current file position to 4, and return the rest of the line: Return the new position: e.g:program f = open("demofile.txt", "r") print(f.seek(4)) Output:4 Unit-1 (Computational Thinking and Programming-2) File Handling : - Getting & Resetting the Files Position seek(): In python programming, within file handling concept seek() function is used to shift/change the position of file object to required position. By file object we mean a cursor. And it’s cursor, who decides from where data has to be read or write in a file. Syntax: f.seek(offset) #here f is file handler or file object #here offset is postions to move forward Explanation: We opened “test.txt” file in read mode. If file opened in read mode than by default file object or file pointer at beginning ie. 0 position. But here we used seek() function and set its position to 6. It means now our pointer has gone to sixth position, if we start reading then it will start reading from sixth character. Hence our output is “there how are you?”. You can also see in above image (data written in text file) highlighted in yellow that “hello” is skipped because pointer is moved to sixth character using seek() function. Unit-1 (Computational Thinking and Programming-2) File Handling : - Modify a Text file Modify a Text file: Modify a Text file: There is no way to insert into the middle of a file without re-writing it. We can append to a file or overwrite part of it using seek but if we want to add text at the beginning or the middle, we'll have to rewrite it. It is an operating system task, not a Python task. It is the same in all languages. What we usually do for modification is read from the file, make the modifications and write it out to a new file called temp.txt or something like that. This is better than reading the whole file into memory because the file may be too large for that. Once the temporary file is completed, rename it the same as the original file. This is a good, safe way to do it because if the file write crashes or aborts for any reason in between, we still have our untouched original file. Unit-1 (Computational Thinking and Programming-2) File Handling : - Modify a Text file replace string in the same File fin = open("dummy.txt", "rt") data = fin.read() data = data.replace(‘my', ‘your') fin.close() fin = open("dummy.txt", wt") fin.write(data) fin.close() What have we done here? Open file dummy.txt in read text mode rt. fin.read() reads whole text in dummy.txt to the variable data. data.replace() replaces all the occurrences of my with your in the whole text. fin.close() closes the input file dummy.txt. In the last three lines, we are opening dummy.txt in write text wt mode and writing the data to dummy.txt n replace mode. Finally closing the file dummy.txt. Note :- above program is suitable for file with small size. Unit-1 (Computational Thinking and Programming-2) File Handling : - Modify a Text file import os f=open("d:\\a.txt","r") g=open("d:\\c.txt","w") for text in f.readlines(): text=text.replace('my','your') g.write(text) f.close() g.close() os.remove("d:\\a.txt") os.rename("d:\\c.txt","d:\\a.txt") print("file contents modified") Note- above program is suitable for large file.Here line by line is being read from a.txt file in text string and text string been replaced ‘my’ with ‘your’ through replace() method. Each text is written in c.txt file then close both files ,remove old file a.txt through remove method of os module and rename c.txt(temporary file) file with a.txt file. After execution of above program all occurances of ‘my’ will be replaced with ‘your’.For testing of above program create a.txt file in d: drive with some substring as ‘my’.Run above program and check changes. Unit-1 (Computational Thinking and Programming-2) File Handling : - Append content to a Text file Append Only (‘a’): Open the file for writing. Append and Read (‘a+’): Open the file for reading and writing. # append from new line file1 = open("myfile.txt", "w") L = ["This is Delhi \n", "This is Paris \n", "This is London"] file1.writelines(L) Output: file1.close() # Append-adds at last Output of Readlines after appending # append mode file1 = open("myfile.txt", "a") This is Delhi # writing newline character This is Paris This is London file1.write("\n") Today Tomorrow file1.write("Today") # without newline character file1.write("Tomorrow") file1 = open("myfile.txt", "r") print("Output of Readlines after appending") print(file1.read()) print() file1.close() Unit-1 (Computational Thinking and Programming-2) Standard Input, Output, and Error Standard Input, Output, and Error:The interpreter provides three standard file objects, known as standard input, standard output, and standard error, which are available in the sys module as sys.stdin, sys.stdout, and sys.stderr, respectively. stdin is a file object corresponding to the stream of input characters supplied to the interpreter. stdout is the file object that receives output produced by print.stderr is a file that receives error messages. More often than not, stdin is mapped to the user’s keyboard, whereas stdout and stderr produce text onscreen. The methods described in the preceding section can be used to perform raw I/O with the user. For example, the following function reads a line of input from standard input: def gets(): text ... Unit-1 (Computational Thinking and Programming-2) File Handling : - Operations in Binary file Operations in Binary file Unit-1 (Computational Thinking and Programming-2) File Handling : - Write and read a Binary file Write and read a Binary file Example: Write to a Binary File Copy f=open("binfile.bin","wb") num=[5, 10, 15, 20, 25] arr=bytearray(num) f.write(arr) f.close() Writing to a Binary File The open() function opens a file in text format by default. To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable. When opened using any text editor, the data is unrecognizable. The following code stores a list of numbers in a binary file. The list is first converted in a byte array before writing. The built-in function bytearray() returns a byte representation of the object. Unit-1 (Computational Thinking and Programming-2) File Handling : - Binary file Operation using pickle module Binary file Operation using pickle module : The problem with the approach of previous slide comes from the fact that it is not very easy to use when we want to write several objects into the binary file. Binary file Operation using pickle module :For instance, consider what we would have to do if we wished to write string, integer and perhaps even the contents of a list or dictionary into the file. How would we read the contents of this file later? This is not a trivial task, especially if some of the objects can have variable lengths. Fortunately, Python has a module which does this work for us and is extremely easy to use. This module is called pickle; it provides us with the ability to serialize and deserialize objects, i.e., to convert objects into bitstreams which can be stored into files and later be used to reconstruct the original objects. Unit-1 (Computational Thinking and Programming-2) File Handling : - Binary file Operation using pickle module Binary file Operation using pickle module : Binary file Operation using pickle module :pickle.dump() function is used to store the object data to the file. It takes 3 arguments.First argument is the object that we want to store. The second argument is the file object we get by opening the desired file in write-binary (wb) mode. And the third argument is the key-value argument. This argument defines the protocol. There are two type of protocol – pickle.HIGHEST_PROTOCOL and pickle.DEFAULT_PROTOCOL. Pickle.load() function is used to retrieve pickled data.The steps are quite simple. We have to use pickle.load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode. Unit-1 (Computational Thinking and Programming-2) File Handling : - Binary file Operation using pickle module Pickling: The process of converting the structure (lists and dictionary etc.) into a byte stream just before writing to the file. This is also called as object serialization. dump() function: We use dump() method to perform pickling operation on our Binary Files. It returns the object representation in byte mode. The dump() method belongs to pickle module. Unit-1 (Computational Thinking and Programming-2) File Handling : - Binary file Operation using pickle module #pickling_in_python import pickle pickle.dump(object,file) #Example_pickling_in_python import pickle def write(): file = open("binary.dat",'wb') x = [1,2,3,4,5] #data we wrote in file pickle.dump(x,file) file.close() write() Output data stored in file: Note: Hence we can observe the output that what data we wrote and what was inserted in our file. Actually data stored is correct but it has been stored in binary format which is not readable by humans. Unit-1 (Computational Thinking and Programming-2) File Handling : - Binary file Operation using pickle module Unpickling: The reverse conversion of byte stream back to the structure (lists, dictionary, tuples etc.) refers to unpickling. Basically it is the reverse operation of pickling. This is also called deserialization. We use load() method for unpickling. This load() function/method also belongs to pickle module. load() function: In pickle module, load() function is used to read data from a binary file or file object Unit-1 (Computational Thinking and Programming-2) File Handling : - Binary file Operation using pickle module Syntax: #Syntax_unpickling_in_python import pickle pickle.load(file) #Example_unpickling import pickle def read(): file = open("binary.dat",'rb') data = pickle.load(file) file.close() print(data) read() Output:[1, 2, 3, 4, 5] Unit-1 (Computational Thinking and Programming-2) File Handling : Search record in a Binary file Here value of r to be searched will be compared with rollno value of file in each iteration / next record and if matches then relevant data will be shown and flag will be set to true other wise it will remain False and 'No record found message will be displayed' Search record in a Binary file-pickle module def check(): import pickle f = open('d:/student.dat','rb') flag = False r=int(input(“Enter rollno to be searched”)) while True: try: rec = pickle.load(f) if rec['Rollno'] == r: print('Roll Num:',rec['Rollno']) print('Name:',rec['Name']) print('Marks:',rec['Marks']) flag = True except EOFerror: break if flag == False: print('No Records found') f.close() Unit-1 (Computational Thinking and Programming-2) File Handling : Update record of a Binary file Here we are reading all records from binary file and storing those in reclst list then update relevant roll no /marks data in reclst list and create /replace student.dat file with all data of reclst. If large data are in student.dat file then it’s alternative way can be using temporary file creation with corrected data then using os module for remove and rename method (just similar to modification of text file) Update record of a Binary file-pickle module import os import sys import file input f = open('d:/student.dat','rb') reclst = [] r=int(input(“enter roll no to be updated”)) m=int(input(“enter correct marks”)) while True: try: rec = pickle.load(f) reclst.append(rec) except EOFError: break f.close() for i in range (len(reclst)): if reclst[i]['Rollno']==r: reclst[i]['Marks'] = m f = open('d:/student.dat','wb') for x in reclst: pickle.dump(x,f) f.close() Unit-1 (Computational Thinking and Programming-2) File Handling : Delete record of a Binary file Here we are reading all records from binary file and storing those in reclst list then write all records except matching roll no (with the help of continue statement) in student.dat file. Due to wb mode old data will be removed. If large data are in student.dat file then It’s alternative way can be then It’s alternative way can be for remove and rename method (just similar to deletion from text file) Delete record of a Binary file pickle module import os f = open('d:/student.dat','rb') reclst = [] r=int(input(“enter rollno to deleted)) while True: try: rec = pickle.load(f) reclst.append(rec) except EOFError: break f.close() f = open('d:/student.dat','wb') for x in reclst: if x['Rollno']==r: continue pickle.dump(x,f) f.close() Unit-1 (Computational Thinking and Programming-2) File Handling : CSV FILE (Comma separated value) CSV FILE (Comma separated value):CSV (Comma Separated Values) is a file format for data storage which looks like a text file. The information is organized with one record on each line and each field is separated by comma. CSV File Characteristics One line for each record Comma separated fields Space-characters adjacent to commas are ignored Fields with in-built commas are separated by double quote characters. When Use CSV? When data has a strict tabular structure To transfer large database between programs To import and export data to office applications, Qedoc modules To store, manage and modify shopping cart catalogue Unit-1 (Computational Thinking and Programming-2) File Handling : CSV Advantages CSV FILE (Comma separated value) Advantages :- CSV FILE (Comma separated value) Advantages :CSV is faster to handle CSV is smaller in size CSV is easy to generate CSV is human readable and easy to edit manually CSV is simple to implement and parse CSV is processed by almost all existing applications CSV Disadvantages No standard way to represent binary data There is no distinction between text and numeric values Poor support of special characters and control characters CSV allows to move most basic data only. Complex configurations cannot be imported and exported this way Problems with importing CSV into SQL (no distinction between NULL and quotes) Unit-1 (Computational Thinking and Programming-2) File Handling : Write / Read CSV FILE using open() method which create new File like object. Through csv.writer() method ,we create writer object to call writerow() method to write objects. Similarly for reading ,we open the file in ‘r’ mode and create new File like object, further we create newfilereader object using csv.reader()method to read each row of the file. Three file opening modes are there ‘w’,’r’,’a’. ‘a’ for append After file operation close ,opened file using close() method. Write / Read CSV FILE Writing and reading operation from text file is very easy. First of all we have to import csv module for file operation/method call. For writing ,we open file in ‘w’ writing import csv #csv file writing code with open('d:\\a.csv','w') as newFile: newFileWriter = csv.writer(newFile) newFileWriter.writerow(['user_id','beneficiary']) newFileWriter.writerow([1,'xyz']) newFileWriter.writerow([2,'pqr']) newFile.close() #csv file reading code with open('d:\\a.csv','r') as newFile: newFileReader = csv.reader(newFile) for row in newFileReader: print (row) newFile.close() Unit-1 (Computational Thinking and Programming-2) Board Questions Q.What is the difference between "w" and "a" modes? Ans."w" mode :- If file exists, python will delete all data of the file. "a" mode :- If file exists, the data in the file is retained and new data being written will be appended to end. Unit-1 (Computational Thinking and Programming-2) Board Questions Q.How is file open() function different from close() function ? Ans. open () function is a built in function while close () function is a method used with file handle object. Unit-1 (Computational Thinking and Programming-2) Board Questions Q.What role is played by file modes in file operations? Describe the various file mode constants and their meanings. Ans. File mode refers to how the file will be used once it's opened.There are three types of file modes:(1) read mode :- It will read data of file according to program. (2) write mode :- Python delete all data and write new data according to user. (3) append mode :- The data in the file is retained and new data being written will be appended to end. Unit-1 (Computational Thinking and Programming-2) Board Questions Q.What is following code doing? file = open("contacts.csv", "a") name = input("Please enter name.") phno = input("Please enter phone number.") file.write (name + "," + phone + "\n") Ans.It will take data from user and write data in file ‘contacts.csv’ Unit-1 (Computational Thinking and Programming-2) Board Questions Q.Write a method in python to write multiple line of text content into a text file mylife.txt line. Ans. file = open("mylife.txt", "w") l = [] while True: data = input("Enter the contents otherwise, Enter ""exit"" for stop the program :") if data == 'exit': break else : l = l + [data + '\n'] file.writelines (l) file.close () Unit-1 (Computational Thinking and Programming-2) Board Questions Q.Write a program to count the number of upper- case alphabets present in a text file "Article.txt". Ans. count = 0 file = open("pathwala.txt","r") sen = file.read() for i in range ( len(sen) ) : if sen[ i ].isupper() : count += 1 print("Number of upper case alphabet : ", count) file.close() Unit-1 (Computational Thinking and Programming-2) Board Questions Q.Write a program to count the words "to" and "the" present in a text file "Poem.txt". Ans. to_no = 0 the_no = 0 file = open("Poem.txt", "r") lst = file.readlines() for i in lst : word = i.split() for j in word : if j == "to" : to_no += 1 elif j == "the" or j == "The" : the_no += 1 print("Number 'to' : " , to_no) print("Number 'the' : " , the_no) file.close() Unit-1 (Computational Thinking and Programming-2) Board Questions Q.Write a program that copies one file to another. Have the program read the file names from user ? Ans.file = input("Enter the name of file with its formate : ") old = open( file , "r") new = open("New file.txt", "w") data = old.read() new.write( data ) print(" Program run successfully ") old.close() new.close() Unit-1 (Computational Thinking and Programming-2) Board Questions Board Questions Q. Write a program in python to replace all word ‘the’ by another word ‘them’ in a file “poem.txt”. Ans . f=open(“poem.txt”) d=f.read() d = d.replace(“the”,them”) f.close() f=open(“poem.txt”,w”) f.write(d) f.close() Unit-1 (Computational Thinking and Programming-2) Board Questions Q.Write a function search(name) in Python which will display record of a student from file “school.dat” whose name is passed as an argument. Structure stored in “school.dat” is in the form of list containing information like [rollno , name, class, percentage] Ans. def search(nm): f=open(“school.dat”,”rb”) try: while True: d=pickle.load(f) if d[1].lower()==nm.lower(): print(“Roll Number:”,d[0],”\n”) print(“Name :”,d[1],”\n”) print(“Class :”,d[2],”\n”) print(“Percentage :”,d[3],”\n”) except: f.close() nm=input(“Enter name of the student”) search(nm) Unit-1 (Computational Thinking and Programming-2) Board Questions Q.A text file ”PARA txt” contains a paragraph Write a function that searches for a given character and reports the number of occurrence of the character in the file. Unit-1 (Computational Thinking and Programming-2) Board Questions Q.Write a function in Python to count the number of lowercase and uppercase characters in a textfile “Book, txt”. Unit-1 (Computational Thinking and Programming-2) Board Questions Unit-1 (Computational Thinking and Programming-2) Board Questions Q.Write a python code to find the size of the file in bytes, number of lines and number of words.# reading data from a file and find size, lines, words f=open(“Lines.txt’,’r’) Unit-1 (Computational Thinking and Programming-2) Board Questions Q.What is the difference between Text file and binary file ?