100% found this document useful (1 vote)
1K views

Chapter 5 Text and Binary File Handling

homlesss buhaadsf

Uploaded by

Toyota Hackathon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
1K views

Chapter 5 Text and Binary File Handling

homlesss buhaadsf

Uploaded by

Toyota Hackathon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 36
5.8 Working with Binary Files Working with CSV Files uter programs work with files. This is because files help in storing information Word processors create document files # Database programs create files of files. So, we see itis the files ; Compilers read source files and generate executabl ofl i itself sa bunch of aes stored on some Tanguage offers some Proviso worked with, inside programs: “/ if ive etc. EV y programming 4in this chapter, you shall ams. Python js no exception an gh Python programs. COMPUTER SCIENCE WITH PYTHON 5.2 DATA FILES The application, for later use toa ic 8. The data files are the files that store data pertaining specifi data files can be stored in feo ways | as | © Text files > te 1. Text Files ofa stream of ASCII or Unicode characters (1). A text file stores information in the form in text files, each line of text is termin, aetna Ren era ihe OF ie ating System) known as EOL (End of 1. Monee nee Sana ternal translations take place when this EOL characteris rea a ee default, this EOL character is the newline character (\\,)) bernie Rie contra (\r\n’)- The text files can be of following types : ae Regular les, These are the text files which store the text in the same form , 2 typed. es character ends a line and the text translations take place. Th... files have a file extension as -txt. (i) Delimited Text files. In these text files, a specific character is stored to separate hy values, ie,, after each value, e.g., a tab or a comma after every value. © Whena tab character is used to separate the values stored, these are called TSV file, (Tab Separated Values files). These files can take the extension as txt or coy © When the comma is used to separate the values stored, these are called CSV files (Comma Separated Values files). These files take the extension as .csv. For instance, have a look at following example : R 2 ontent: | am simple text. Cer oe, F The CSV (Comma Separated Values) format @ popular import and export format for ene ex. spreadsheets and databases Most commonly used delimiter in a CSV files files (e.g., .INI files) and rich text format ®™ma(,), butt can also use other delim te: files (RTF files) are also text files. Characters like tab (—>), pipe (|), tide ete. Iam > simple = text. 2. Binary Files A binary file stores the information in the form of a stream of bytes. A binary file contai information in the same format in which the information is held in memory, i, the file cont that is returned to you is raw (with no translation or no specific encoding). In binary f is no delimiter for a line. Also no translations occur in binary files. As a result, binary faster and easier for a program to read and write than are text files. As long. as the file does need to be read by people or need to be ported to a different type of system, binary files are the best way to —ee7=— store program information. Be co be opened In ary 124 The binary files can take variety of extensions. Most editor and are in human readable fo™ wa non-text file extensions (any application defined inary fles are notin human ead30 extension) are binary files. le No tak a Python program, you need to open Hn 8 specie a“ nr aan, at erm The te pt petaiong ata 10 Whe, which in turn inetd any one of the first thing that lowing he You do is open the file. It done using > = open() 2 * OPEN , ) MyFile = open(“taxes.txt") <—— aad aad le2 = open(“data.txt”, "r”) ibove statement opens the file “data.txt” in read to file object namely file2, ‘one more file-open statement : "Filed = open("e:\main\\result. txt", "w') <—— Pret mi ok for th le mode (because of “r’ given as mode) and ‘statement opens file “result.txt” (stored in folder invite mode (because of" givens mode) nt to file object namely file3, (Aeticonete ee: we above file open statement, the file path contains Metandle, & hes in place of single slashes.) given three file-open statements must have raised these questions in your mind is file-object? is mode or file-mode ? lines will have answers to all your questions, but for now, let us summarize the file 's open( ) function creates a file object which serves as a link toa file residing on your COMPUTER SCIENCE WITH PYTHOn, ) function is a path t st the file name le in the current © The first parameter for the opent the file you'd like to open. If given, then Python searches for folder. © The second parameter of the open ee corresponds to a mode which is typically read (") write ('w’), or append (‘a’). If no second par given, then by default it opens it in read (‘r) mode. Figure 5.1 summarizes the open ) function of Python for you. Fle object Path to file wie £ = open(“cz\\temp\\data.txt”, "r') Figure 5.1. Working of file open( ) function. As you can see in Fig. 5.1, the slashes in the path are doubled. This is because the slashes have special meaning and to suppress that special meaning escape sequence for slash i., \\ is given. However, if you want to write with single slash, you may write in raw string as : with r here, you can give single. ————_. elites paket f =open(r"“c :\temp\data. txt", “t") ie The prefix r in front of a string The prefix r in front of a string makes it raw string that makes it raw string that means means there is no special meaning attached to any character. there is no special meaning Remember, following statement might give you incorrect _ attached to any character. oat! aE f = open("c :\temp\data. txt”, "t") «__ ee Reason being that ‘will be treated as tab character and ESSERE ‘\d’ on some platforms as numeric-digit or some error. Thus the ftvo ways to give paths in filenames correctly are : re ee epee rode is read ue pre pel Ta mode, ie, if you do not provide any (i) Double the slashes e.g., file open mode, Python will open it # = open("c:\\temp\ \data. txt", "r") eer. (i) Give raw string by prefixing the file-path string with an r £8. = open(r"c: \temp\data. txt", "r") "éata,txt” rather than “c\\tem\data tt” Object/File Handle are used 10 read and write date to 0 file b d hw om dink. object is used to obtain a reference to the file on disk ence’ it for a number of different tasks a ee ne (also called file-handte) is very im /ery Important and useful as through a file-object only, a Python program can work files stored on hardware, All the functions that you non a data file are performed through file-objects n you use file open( ), Python stores the reference of mentioned file in the file-objed. abject of Python is a stream of bytes where the data can be read either byte by byte oF line BY ine or collectively. All this will be clear to you in the coming lines (under topic ~ File access modes). Access Modes Python opens a file, it needs to know the file-mode in w! de governs the type of operations (such as read or write or append) {f refers to how the file will be used once it’s opened. File modes hich the file is being opened. A possible in the opened supported by Python Fe i Sean ge pea seed % Default mode ; File must exist already, otherwise Python raises I/O error. © If the file does not exist, file is created. @ If the file exists, Python will truncate existing data and over- write in the file, So this mode must be used with caution. # File is in write only mode. © If the file exists, the data in the file is retained and new data being written will be appended to the end. Ifthe file does not exist, Python will create a new file. ¢ File must exist otherwise error is raised. Both reading and writing operations can take place. # File is created if does not exist. * If file exists, file is truncated (past data is lost) & Both reading and writing operations can take place, * File is created if does not exist. * If file exists, file's existing data is retained appended. ‘e Both reading and writing operations can take place ;mew data is COMPUTER SCIENCE WITH Prnycy, To create a file, you need to open a file in a mode that supports write mode (ie, ‘Ww’, Ota’ OF “we or ‘ae modes), 5.3.2 Closing Files ject. Closing of file is itnpor, An open file is closed by calling the cfose(,) method of its flestiet one | In Python, files are automatically closed at the end of the program a system may not chon : the habit of closing your files explicitly. Why ? Well, the opera i ees data out tothe file uni itis close (this can boost performance). What fh PO Gel have 1 Program exits unexpectedly there is a danger that your precious .close( ) : feen eo and the file on the dic, For instance, if a file Master.txt is opened via file-handle ilose( ), no tasks can be ; 0 ; aR outfile, it may be closed by the following statement : hs that — th outfile.close( ) <—— ee aoe Please remember, open( is a built-in function (used standalone) while close( ) is a method use: with file-handle object. 5.4 WORKING WITH TEXT FILES Python provides many functions for reading and writing the open files. In this section, we are going to explore these functions. Most common file reading and writing functions are being discussed in coming lines. 5.4.1 Reading from Text Files Python provides mainly three types of rad functions to read from a data file, But before you ca read from a file, the file must be opened and linked via a file-object or file handle. Most common file reading functions of Python are listed below in Table 5.2. Table 5.2 Python data files — reading writing functions ‘SNo.| Method | ‘Syntax Pe Mieerpen E 1. | read() .read([{n]) reads at most n bytes ; if no # is Specified, reads the entire file, Returns the read bytes in the form of a strin >>> filel = open("E:\\mydata\ \info. txt >>> readInfo = filel.read(15) >>> print(readInfo It's time to wo 15 bytes read >>> type(neadInfo) str > Fllel = open(*E:\\mydata\\info.txt") >»? readinfo = filel.readline() >>> print(readtnfoy It's time to work with files, reads all lines and returns them in a list >>> file = open("e:\\mydata\\info. txt") >>? readinfo = filel.readline() | >>> print(readinfo) | m4 | ("Te's time to work with files.\n", “Files offer and fan ant Power to store your work/data/informaticn | for later use.\n", “siaply create a file and Share(write) tn At \n*, “Or open an existing File | and read from i.\n"] | >>> type(neadinfo) | iehar ce» in above syntaes is the fil-objet holding open He’ veloreac, | Oe ako nanples now. For the examples and explanations below, we are using a poem. txt storing the content shown in Fig, 5.2 WHY 7 ‘We work, we try to be better ‘We work with full zest But, why Is that we just don't know any letter We still give our best. We have to steal, But, why is that we still don't get a meal ‘We don't get luxury, We don't get childhood, But we still work, Not for us, but forall the others. Why is it that some kids wear shoes, BUT we make them ? by — Mythil, class § do not want to have the newline characters inserted by prin statement in the output then argument inthe end of prin) statement so that print does not put any additional ' nt in the output. Refer to code snippets 3 and 4 below. 3 Reading a file's entire content myfile = open(r'é :\poem.txt’, "r”) str=myfile.read()«— ' eet aoe myfile.close() s output produced by above code is : car You can also combine the open} and read) functions as: File("filenane”, ) read ) We work, we try to be better Shed ation bse one We work with full zest — But, why is that we just don’t know any letter. We still give our best. We have to steal, ‘But, why is that we still don't get a meal. | We don't get Tuxury, | We don’t get childhood, | But we still work, Not for us, but for all the others. Why is it that some kids wear shoes, BUT we make them ? by mythili, class 5 myfile = open(r'E:\poem.txt', "r") str = myfile.readline() print(str, end="") str = myfile.readline() Petes Oe tL Acree Argamet end “at the end, Of prini() statement str =myfile.readline() ‘will ensure that output shows exact content of the ve data file and rint-inserted newline characters are print(str, end="") Re myfile.close() ee ie ee ie HS TE ea for loop’s variable moves through the file line by line where a line ofa file is considered as @ “sequence of characters up to and including a special character called the newline character ('\r). _Sothe for loop's variable starts with first ine and with each iteration, it moves to the next line. As the for loop iterates through each line of the file the loop variable will contain the current line of _the file as a string of characters, & Displaying the size of a file after removing EOL characters, leading and trailing white spaces and blank lines myfile = open(ré:\poem. txt’, "e") stri="" #initially storing a space (any non-None value) size=0 tsize=0 while stra : str1 = myfile. readline() tsize = tsize+len(str1) size = size + len(stri.strip()) print ("Size of file after renoving all EOL characters & blank lines:", size) print ("The TOTAL size of the file:", tsize) myfile.close() e output produced by the above code fragment is: Size of file after removing all EOL characters & blank lines : 360 ‘The TOTAL size of the file : 387 mmyfile = open(E:\poen.txt’, "e") ‘S$ =myfile.readlines() *———— noice itis readlines( ) not readline ) print(s) myfile.close() look at the output. The readlines( ) has read the entire file in a list of strings ‘wHY?\n", '\n', ‘We work, we try to be better\n', ‘we work with zest\n', “But, why is that we just don’t know any letter.\n", ', "We still give our best.\n’, ‘we have to steal,\n', “But, why we still don’t get ameal.\n”, '\n', “we don’t get luxury, don’t get childhood,\n”, ‘But we stil] work,\n', ‘Not for for all the others.\n', ‘\n', ‘why is it that some kids wear we make them ?\n', by MythiTi, class 5\n'] puTER SCIENGE WITH PYTHON ~ 1) Now that you ae fanstir with these reading functions’ working let we write some Programs P 1 Wrive « program to display the sie of a file in bytes myfile © open(ré:\poem, txt’, "r") str =myfile.read() rr. size = len(str) size of the given file poem.txt is print ("Size of the given File poem. txt 1s") 387 bytes print(size, "bytes") ‘Write a program to display the number of lines in the file myfile = open(rE:\poem.txt’, "r") = myfile.readlines() Linecount = len(s) print ("Number of Lines in poem.txt is", linecount) myfile.close() >>> number of lines in poem.txt is 18 4.2 Writing onto Text Files After working with file-reading functions, let us talk about the writing functions for data files "available in Python. (See Table 5.3) Like reading functions, the writing functions also work on ‘open files, ie,, the files that are opened and linked via a file-object or file-handle. Python Data Files ~ Writing Functions ite() _| .write(str1) writes all strings in list L as lines to file refe renced by en you open a file in “w” or write mode, Python overwrites an existing file or creates 0 g file. That means, for an existing file with the same name, the earlier data gets lost wer, you want to write into the file while retaining the old data, then you should open “a” or append mode. A file opened in append mode retains its previous data while add newer data into. You can also add a plus symbol (+) with file read mode to the file has been opened in append mode (“a”) to retain the old content. f the file has been open in ‘r+’ or ‘a+’ modes to facilitate reading as well as writing (9) ifthe file has been opened in write-only mode (“w”) 11) ifthe file has been open in ‘w+’ mode to facilitate writing as well as reading Make sure to use close() function on file-object after you have finished writing as ‘sometimes, the content remains in memory buffer and to force-write the content on file and closing the link of file-handle from file, close( ) is used. at us consider some examples now. 8 Create a file to hold some data Fileout = open("Student dat", “w") for i inrange(5) : Name = input ("Enter name of student :") fileouturste(nane) aay fileout.close() The write() will simply write the content in file without adding any extra Its important to use close() ate : Enter name of student : Riya r name of student : Rehan Enter name of student : Ronag "i Enter name of student : Robert Enter name of student : Ravneet. an see the file created in the same folder where this Python script/program is saved 53(a) below). However, if you want to create the file in specific folder then you must the file-path as per the guidelines discussed earlier, ou can open the created file (“student.dat” in above case) in Notepad to see its contents, b) shows the contents of file created through code snippet 5. The data you enterec is part ‘Of the file. BUT notice that write) does not add anewine character on its own (0) File created through open( ) with “w" mode is stored in the same folder as that of script fle (0) The write( ) function does not add any extra character in the file, COMPUTER SCIENCE WITH PYTHON — x Code Snippet 9 Create a file to hold sonte data, separated us lines ; —_— ith code snippet 8) (This code is creating a different file than created with code ®tP! fileout = open("studenti. txt”, “W") for i in range(5) : name = input ("Enter name of student :") Fileout .write(name) . ein “yn? written afer #Lleout..write(’\nt) <—— Te newline charter“ fileout.close() ‘The sample run of the above code is as shown below : > Enter name of student : Jaya newline characters at the Enter name of student : Jivin ee enter name of student : Jonathan you added a newine “esa after Enter name of student : Jagjee every name Enter name of student : Javed aah Sonachen ‘See this time the file has. The file created by code snippet 9 is shown above. Code snippet 10 Creating a file with some names separated by newline characters without usin TTC Pe function: (For this, we shall use writelines( ) inplace of write() function which writes the content ofalist to afile. Function writelines() also, does not add newline character, so you have t take care of adding newlines to your file.) fileout = open("Student3. txt", "w') List =[] for i in range(5) : nane = input ("Enter name of student :") Uist append(name +"\nt) #—_ Responsibility to add newline fileout .writelines(List1) shharacter is of programmer Fileout .close() Sample run of the above code is as shown below i Nitya + Noor 3 Nathan + Naved 2 Navin are familiar with these writing functions’ working, let us write some programs ‘on the same, Wee @ program to get roll numbers, na ‘store these details in a file called *Marks.ox and marks of the students af a clase (wet from usec) aed count * Ant (input ("How many students are there in the ¢ FAleout = open (Marks, txt", "w") 95?")) for 1 in range(count) : print(“Enter details for student", (141), "below:") Pollno = int(input("Rolino: ))" name = input("Name ;") marks = float(input ("Marks :")) rec = str(rollno) +","+name +","+ str(marks) +\n" fileout .write(rec) Joining individual information by #ileout .close() SST ein come Be y students are there in the class? 3 Fle Edit Format, View Help |32,haze1,67.75 z 45, diya, 1 | 16, Noor, a File created by ks : 78.5 ter details for student 3 below : Ino: 16 Figure 5.4 file created through program. carefully notice, we have created comma separated values in one student record while in file, So we can say that the file created by program 5.4 is in a format similar to CSV separated values) format or it is a delimited file where comma is the delimiter Write a program to add two more students’ details to the file created in program 5.3 a ks .txt" see ees Notice the file is opened in append mou ge ren i ) (“a”) this time so as to retain old for i in range(2) : We want to ad print (“Enter details for student", (i+1), "below :") Ei" rollno = int(input(“Rollno:")) name = input("Name :") marks = float (input ("Marks:")) rec = str(rollno) +","+name+"," + str(marks) +\nt fileout .write(rec) .close() ‘enter a Rolling ; Name: saat marks : 78.9 Enter ee for RolIno ; Name : sk Marks : 89.5 for student 1 below : ‘Student 2 below : on + created through » P 5.5 Write a program {0 display the contents of file “Marks.tat” c Outpet rogram fileinp = open(Marks. txt", "r" ‘ while str : ‘ Str = fileinp.readline() & print(str) , fileinp.close() Have a look at some text file Fig, 5.5) File : Answer.txt Letter ‘a is a wonderful letter. {tis impossible to think of a sentence without it, We know this will never occur, How mediocre our world would be without this single most powerful letter. Figure 5.5 The contents of file Answer.txt. ‘rite a program to read a text file line by line and di myfile = open("Answer.txt", " line ="* while line : Line = myfile, readline() #one line read from file # printing the Line word by word using split() for word in line. split(): Print(word, end = 'g') print() close the file myfile.close() i ‘S#atwonder ful: #etter.¢ blePeoe think isplay each word separated by a n) #initially stored a ‘Space (a non-Non, alue @ program to read a text file and display the count of vowels and consomants i the file. ‘= open("Answer.txt", "r") initially stored a space (a non-None value) variable to store count of vowels variable to store count of consonants Muhile ch stores a Non-None value ch=myfile.read(1) #one character read from file Mfchin{'a', 'A', ‘e', 'E', 4, 'T', ‘0°, 0", ‘u's U'Te vcount = vcount +1 else: count = ccount + 1 ("Vowels in the file :", vcount) ‘Consonants in the file :", ccount) ‘the file .close() ish( ) Function 5u write onto afile using any of the write functions, Python holds everything to write in buffer and pushes it onto actual file on storage device a later time. If however, you Python to write the contents of buffer anto storage, you can use flush() function. matically flushes the file buffers when closing them ie, this function is implicitly the close( ) function. But you may want to flush the data before closing any file, The lush( ) function is : The flush( ) function forces the | writing of data on disc still) pending in output buffer. | n( ‘out. Log’, 'w+") (‘The output is \n') ( "My" + "work-status "+" is") With this statement, the strings written so far, gC The ups and My wor is have been pushed on to actual file on disk ea These write statements’ strings may still be pending to be writen on to disk + Tie flush( ) function ensures that whatever is held in ‘Output buffer, is written on to the actual file on disk d data from file and return it in string where each line is one d readline( ) functions discussed above, rea »() function returns the entire file content ina list COMPUTER SCIENCE WATH FTHON x stespaces Le, spaces oF tabs or All these read functions also read the leading and trailing ‘and leading whitespaces, yo. newline characters. Ifyou want to remove any of these ak EN can use strip() functions [rstrip(), Istrip( ) and strip( )} &8 Recall that : © the strip( ) removes the given character from both © the rstrip( ) removes the given character from trailing © the lstrp( ) removes the given character from leading end #4 To understand this, consider the following examples : 1. Removing EOL ‘n’ character from the line read fh=File("poem.txt, "e") Will remove the end of line Line = fh.readline() 3 line = Line.rstrip(‘\n') ends. end i, right end. left end. from the file. newline character ‘\”" 2. Removing the leading whilespaces from the line read from the file line = file("poem.txt, "r") readline() line = line. Istrip() Now can you justify the output of following code that works with first line of file poem.text shown above where first line containing leading, 8 spaces followed by word ‘WHY? and a’\n' in the end of line. Following lines list the steps that need to be followed in the order as mentioned below. The five steps (0 use files in your Python program are : 1. Determine the type of file usage Under this step, you need to determine whether you need to open the file for reading purpose (input type « ‘usage) or writing purpose (output type of usage). Ifthe data is to be brought in froma file to memory, then file must be opened in a mode that supports reading such as “r® or “r+” etc. ‘Similarly, if the data (after some processing) is to be sent from memory to file, the file must be opened ‘mode that supports writing such as “w” or “w+” or “a” etc, 2. Open the file and assign its reference to 2 file-object or file-handle Next, you need to open the file using open ) and assign itto a file-handle on which all the file-operations ». be performed, Just remember to open the file in the file-mode that you decided in step 1. 3. Now process as required ¢ As per the situation, you need to write instructions to process the fleas desited. For example, you mish to open the file and then read it one line at a time while making. ‘computation, and so on. 4. Close the file A This is very important step especially if you have opened the file in write mode, This is hec: the last lap of data remains inbutfer ands not pushed onto disk unl a close ) operation i prt ificance of File Pointer in File Handling ae ean which tells the current position in the file where writing or place. (A file pointer in this context works like a book-mark in a book). oo from the file or write onto a file, then these two things happen “this Operation takes place at the position of file-pointer and | To see \ filepointer advances by the specified number of bytes ce \ 5.6 illustrates this process, Its important to understand how a a) te works in Python files, The working of file-pointer has described in Fig. 5.6. i EL EEbE bb bh ebb an of ile.pointer when file is opened in read mode (*r*), ie, file-pointe les in beginning 14 byte from the file from the position the file-pointer is currently at ; and the file pointer advances by one byte. ow the ch will hold “1” and the file pointer will be at next byte holding value ‘2’ (see below) lst Ts. DTT Pelsl [shell Js). the file-pointer has advanced by 1 position as previous read operation read I byte only. ‘nexd read will take place at the current position of file-pointer. Feith [i from the fle from the position the file-pointer is currently at and the file pointer advances by 2 bytes, ‘str will hold ‘2, and the file pointer will be at next byte holding value 'H’ (see below) PELEEEL TT) Tshels[]. 1s) _ See now, the file-pointer has advanced by 2 positions as previous read operation read 2 bytes only “Now the next read will take place at the curent position of ile-pointer ow 1}, }eol7|-|7|5|\" Figure 5.6 Working of a file-pointer. and the Opening Position of File-Pointer n ofa file-pointer is governed by the filemode it is opened in. Following table lists the on of a file-pointer as per filemode. the a beginning of the file (Overwrites the file if the fil Tr if the file exists otherwise creates a new file for files is very handy when you have two related operations which you'd lke to ‘with a block of code in between, The syntax for using with statement is : i hopen(, ) as + File manipulation statements» ‘example is opening a file, manipulating the file, then closing it : hopen(‘output.txt’, 'w) as f: f.write('Hi there!') at with statement will automatically close the file after the nested block of code. The advantage of j awith statement is that itis guaranteed to close the file no matter how the nested block exits. Even if an (a runtime error) occurs before the end of the block, the with statement will handle it and and Relative Paths name of a file or directory or folder consists of path \ primaryname.extension 1 sequence of directory names which give you the hierarchy to access @ particular ry or file name. Let us consider the following directory structure : ¥ omen (root folder) the top most folder rs) on) = eaees) == oo = e-letter: \directory {\directory...] , ‘refers to root directory and other (’\’s) separate a director ry name from eR as WITH BINARY FILES, ow you have leamt to write lines/strings and lists on files. Sometimes you may need to and read non-simple objects like: dictionaries, tuples, lists or nested lists and so forth on to the Since objects have E Some structure or hierarchy associated, it is important that they are ‘stored in way so that their structure/hierarchy is maintained. For tl this purpose, objects are often serialized and then stored in binary files, © Serialisation (also called into a byte stream so that __ Stream in such a way a ti Pickling) is the process of converting Python object hierarchy tit can be written into a file, Pickling coverts an object in byte that it can be reconstructed in original form when unpickled or ‘© Unpickling is the inverse of P hierarchy. Unpickling produce ‘ickling where a byte stream is converted into an object s the exact replica of the original object. Python Provides the pickle module to achieve this. As per CEs Python's documentation, “The pickle module implements a “Pickling” isthe process whereby Fundamental, but power algorithm for seriatizing and aa ee de-serializing a Python object structure.” Inthis section, you shall" “unplekling” is the inverse n fo "use pickle module for reading/writing Objects in operation, whereby a byte-| ary files. ‘stream is converted back into an object hierarchy. And then, you may use dump() and load() methods! Of pickle module to write and read from an en binary file respectively. Process of working with binary files is similar to as you have been So far with a little difference that you work wit ith pickle module in binary files, ie, Import pickle module. — Amport pickle ling/Closing Binary Files file is opened in the same way Dut make sure to use “b” as you open any other file ( (as explained in section 5.3 with file modes to open a file in binary mode e.g, . — Binary file opened in write mode with file handle as Dfile "* Notice ‘is used with the fle modes tapaggee Binary file opened in read mode with fle handle as Filet oad ) of pickle modu but for an open binary file. But as pes these serialise/de-serialise objects in string per syllabus, we shall only cover only load{ ) ears [ae rego ty is a COMPUTER SCIENCE WITH PYTHON — x output file mode and it does not Like text files, a binary file will get created when opened in an out opened i he file will get created if it does exist already, That is, for the file modes, “w, “w +", “A ee and “w +” will overwrite not exist already but ifthe file exists already, then the oe nic the file and the file mode “a” will retain the contents of any other file ie, as An open binary file is closed in the same manner as you close any Ofile.close() ite/read into binary files. Let us now leam to work with pickle module’s methods to write) e Es eT ee on in, We shall talk about both these methods (reading inside try cone 6 statement) when we talk about reading from binary files in section 5.6.3. 5.6.2 Writing onto a Binary File — Pickling E In order to write an object on to a binary file opened in the write mode, you should use dump\) function of pickle module as per the following syntax : Pickle. dump(, ) For instance, if you have a file open in handle file1 and you want to write a list namely list! in the file, then you may write : Pickle.dump(1ist1, file1) +————_. objet listt is being written on file opened with file handle as Filet Inthe same way, you may write dictionaries, tuples or any other Python object in binary file using dump() function. For instance, to write a dictionary namely student! in a file open in handle file2, you may write : pickle. dump(student1, Oe) ae Object studentt is being written on file opened with file handle as File2 llows you to pickle object. following data types Integers, Floats, Complex Strings, Tuples, Liss, Sets ‘containing picklable and classes’ objects e%: Now consider some example programs given below. 5.8 m Write a program to a binary ? Snpleyees tree eRe employe ce import pickle # dictionary objects tobe stored in the binary File empl = {"Empno’ : 1201, ‘Name* : enp2 = {‘Enpno' : 1221, ‘Name emp3 = {‘Empno’ : 1251, ‘Name emp = {"Empno" : 1266, "Name FIUE HANDUNG a1 #open file in write mode fenpFie = open( imp. dat', yh!) ——— Se Pr write md ad for the Wary fle #write onto the file -_pckle. dump (emp, enpfi.te) Mg Berssiereem2, enotile) |. —————-— Dighany ote rg ert fe aed Pickle.dump(emp3, empfile) ane ae pickle.dump(emp4, enpfile) print( “Successfully written four dictionaries”) ‘empfile.close( ) # close file ___ Theabove program will create a file namely Emp.dat in your program’s folder and if you try to ‘open the created file with an editor such as Notepad, it will show you some garbled values (as __ shown below) because binary files are not in human readable form. 9 Write a program to get student data (roll no., name and marks) from user and write onto a binary fie ‘The program should be able to get data from the user and write onto the file as Jong as the user wants. import pickle stu={} # declare empty dictionary stufile = open('Stu.dat', 'wb') # open file __ #get data to write onto the file ans='y' no = int(input("Enter roll number : ")) ‘name = input("Enter name : ") marks = float(input("Enter marks : ")) Output #add read data into dictionary Prt eice\ fumber:+ 12 cae enter rol] number : uae Enter name : sia ‘Name’ ] = name Enter marks : 83.5 want to enter more records? (y/n)...¥ into the file Enter rol] number : 12 ners nM 1 dump(stu, stufile) Enter name : Guneet beanie Fic lganter: ») Enter marks : 80.5 i Snel) ie als a Gad") ant to enter more records? (y/n)-..Y close e Enter roll number : 13 Enter name : James Enter marks : 81 want to enter more records? (y/n)..-n ene in OED \ ‘These 3 student records are written to the file studat 5.6.24 Appending Records in Binary Files Appendling records in binary files is similar to waiting, only thing you have to ensure is that you must open the file if append mode ("ab"), A file opened in append mode will retain the previous records and append the new ‘written in the file. Just as you normally write in a binary file, You write records while appending using the same duumpt ) funetion of the pickle module, P B10 Wee a program to append sudo reanl 0g een A Tene nr” _ use rogram inport pickle ‘declare empty dictionary stu={} ‘# open file in append mode stufile open("Stu.dat’, "ab") <————— for appending the records, the fil is opened # get data to write onto the file roped made as of he regan sar ein ‘that of writing records. ans = 'y » while ans =="y! : | no = int (input("Enter roll number : ")) | name = input ("Enter nane :") marks = float( input("Enter marks > ") 1 # add read data into dictionary stuf RolIno'] = rno stu['Name"] = name stu[ 'Marks"] = marks # now write into the File pickle.dunp(stu, stufile) ans = input ("Want to append more records? (y/n) #close file stufile.close() ‘The sample run of the above program is as shown below : Enter rol] number : 14 Enter name : Ali Enter marks : 80.5 1 more student record is appended to te ile ss. | want to append more records? (y/n)...n 5.6.3 Reading from a Binary File ~ UnPickling Once you have written onto a file using pickle module’s dump() (as we did in the section), you need to read from the file using load() function of the pickle modu then unpickle the data coming from the file, . toread an object in nemp from afile open i ile-handle fout, you would write: HRP = DACKL. Sad (Foust) —— and fm th le cpa th le hn ft ase the read data in an object namely memp reee tra att does the same for you. It reads the objects written by program 5.8 from the and displays them, But before the program 5.11, read the following box. (Important) {You move onto the prograin ode, iis impo | raise EOF Error (a run time exception) when you reach end-of-file while reading from the file. ‘handle this by following one of the below given two methods, Use try and except blocks © Using with statement Use try ond except blocks - _—— In the try block, write the pickleload( ) = pickle. load() * Satemant and = roccing ‘Statements o In order to read all the reconds, read inside a _ #other processing statenents loop as shown in th owing progam ais ba ~ Use this Keyword for checking EOF ie EOFError : . are — writ 1 what to _ .close() eo aunts , You just need to just concentrate on the Syntax; you need not go in further details of try and ‘itis beyond the scope of the book. is. a compact statement which combines the ‘opening of file and processing of file exception handling, (Refer to Info box 5.2 given earlier where we have talked about ent.) The with statement will also close the fle automatically after with block's ove 1 statement as ; . commen HeMNCE HTH PTHON 11 and 5.12) and In the program below, we have used both the tr except bloc (in rvs By tts the with statement (in programs 5.13 and 5.14) for working With IMe Tr following program that is reading from the file you created in the Prev! P ‘ 4s written in tan 5.11. Wiite a program to open thei imp dt (rated in ram 58), rod te Fe display them, import pickle 4 ‘declare enpty dictionary object; it will hold the read rec emp ={)} mode empfile = open('Emp.dat', 'rb’) # open binary file in read #read from the File try: Read repeatedly; when no more records, it will give HOF pit ae oo eek sh fe sled ini Sen ETETEME | sen the end of file isreached (EOF exception). ‘Ths statement would unpickle the object being read from fil You can now use the object in usual way (e.g. we pr except EOFError: is coment) empfile.close() # close file ‘The statements inthis block will get executed when EOF has occurred, i, the fle will be closed on reaching EOF The output produced by the above program will be : , ‘salary’: 47000} |—~ {"empno': 1201, 'Name': ‘Anushree', ‘Age': {"Empno': 1211, ‘Name’ 30, ‘salary’: 48000} Its returning the same di {'Empno’: 1251, ‘Name tage’: 27, 'Salary': 49000} | as we wrote in the pre . On " " " Te program (Compare {'Empno': 1266, "Name: 9, 'Salary': 50000} snail Now that you have an idea of how to read and write in binary files, let us write a few more programs before we talk about searching in and updating the binary files. P 5.12 Write a program to open file created and used in programs 5.9 and 5.10 and display the records stored in it. import pickle stu ={} : # declare empty dictionary object to hold read records fin =open('Stu.dat’, 'rb') — #open binary file in read mode # read from the file try: print ("File Stu.dat stores these records") while True : # it will become False upon EOF stu=pickle.load(fin) — #read record in stu dictionary from fin file handl: print(stu) # print the read record except EOFError: fin.close() ‘# close file sate a binary file namely myfile.info and write a string having two lines in it. sigwer : i ‘open (“myfile. info", “wb") as fh: File myfile.info is opened in file harclle fh OSes All file processing statements inside the with block. -print(“File successfully created") gram has created binary file namely myfile nfo that stored the given string in the _ Write a program to read from the file myfile.info created in previous program and display the string mil letter “o” is encountered, i.e., display all the text before the letter ‘o’, Peeinee ‘See, the text before the letter ‘o” is displayed Bi ink a ways of searching for a value stored in a file. The simplest being the yhereby you read the records from a file one by one and then look for the We are covering the same method here. That is, in order to search you need to do the following : from the file are being referred to as records in this chapter.) COMPUTER SGIENCE WITH PYTHON — x riable namely found that is Following program is just doing the same. Itis using a Boolean NTE Oa this variable is False initially and stores True, as soon as the search is seers tested for its value and accordingly the message is Feporte®s oll mumbers as 12 oF 14. 1f fou ) 5.15. Write a program to open file Studat and search for records with display the records. | import pickle ad records stu =() H declare empty dictionary object to Nold re found = False iia Fin=open('stu.dat’, ‘rb) _stopen binary file in read searched for searchkeys = [12, 14] # List contains key values to be # read from the file try: print ("Searching in File Stu.dat ...") ‘4 while True : 4 it will become False upon EOF exception stu=pickle.load(fin) _ #tread record in stu dictionary from fin file handle if stu['Rollno'] in searchkeys : <——— Searching for in the read record a ‘the record * ‘This block will get executed when’ the search is successful except EOFError: if found == False: print(""No such records found in the file") area Searching in File Stu.dat ... | print("Search successful.") {'Rollno’: 12, ‘Name’: ‘Guneet’, ‘Marks’: 80.5 fin.close() # close file {'Rolino': 14, "Name": 'Ali', ‘Marks’: 80.5 search successful. 5.16 Read file stu.dat created in earlier programs and display records having marks > 81 rogram import pickle stu = {} # declare enpty dictionary object to hold read records found = False print("Searching in file Stu.dat ...") # open binary file in read mode and process with the with block with open(‘Stu.dat’, 'rb") as fin: stu=pickle.load(fin) — #read record in stu dictionai if stu['Marks'] >81: ry from fin file handle print(stu) # print the read record found = True if found == False : print("No records with Marks >81") § Searching in file stu.dat for marks > 81 rica: TROMnO': 11, ‘ame’: ‘sia’, 'Marks': 83.5 i print ("Search successful.) ‘Search successful. You know that updating an object means changing its value(s) and s it Updatin, i ina file is pale Hor three-step a, which is oe {Locate the record to be updated by searching for it (W) Make changes in the loaded record in memory (the read record) (ii) Write back onto the file at the exact location of old record, ‘You can easily perform the first two steps by whatever you have learnt so far. But for the third _step, you need to know the location of the record in the file and then ensuring that the record oe is written at the exact location. Thus, we shall first talk about in the following A how you can obtain the location of a record and how you can place the file pointer a specific location (requirements of updating in a file). Accessing and Manipulating Location of File Pointer — Random Access hon provides two functions that help you manipulate the position of file-pointer and thus ou can read and write from desired position in the file. The two file-pointer location functions Python are : tell() and seeK( ). These functions work identically with the text files as well as files. ere file-object is the handle of the open file, e.g, if ‘is opened with handle fin, then fin.tell() will > you the position of file pointer in the file ee ae le Edit Format View Help 12,Hazel,67.75 a 15, Jiya,78.5 16,Noor,68.9 nsider some examples, given below. We are using 17,Akshar, 78.9 same file “Marks.txt” (shown on the right) that al created in earlier examples. ow consider the following code snippet : " wae To get current position of file pointer open("Marks.txt", "r") tially file-pointer's position is at:", fh.tell()) ‘bytes read are:", fh.read(3)) 5 bytes read fter previous read, Current position of file-pointer:", ¢h.tell()) d by the above code will be : Initially file-pointer is at Oth byte ter’s position is at : 9 ——— 12, ———__ Notice first 5 bytes of the file contain 1, 2 and a comma jae: Current posith file-pointer : 3 ca \ afer reading 3 bytes . Y file-poiner’s position is 5 mamuen SCIENCE HATH PYTHON — 1 or Now consider the following modified code : fh open("Marks.txt", "ry print ("3 bytes read ar fh. read(3)) o", fh.tell()) , Current position of file-pointer:"s fP Print ("Next 5 bytes read:", fh.read(5)) nter:”, fh.tell()) print (“After previous read, Current position of file-po% The output produced by above code will be : >>> 1 3 bytes read are: 12, Returned by first fh-tell( ) ; ters 3 z after previous read, current position of file-pointer: Next 5 bytes read: Hazel oat Returned by second fu.tell/ ) After previous read, current position of file-pointer: The seek( ) Function The seek( ) function changes the position of the file-pointer by placing the file-pointer at the specified position in the open file. The syntax for using this function is : .seek( offset[, mode] ) where offset is a number specifying number-of-bytes mode is a number 0 or 1 or 2 signifying 0 for beginning of file (to move file-pointer w.r.. beginning of file) itis default position (ie., when no mode is specified) 1. for current position of file-pointer (to move file-pointer w.r.t. current position of it) 2. for end of file (to move file-pointer wt. end of file) file object is the handle of open file. The seek( ) function changes the file pointer’s position to a new file position ~ star! + of/r with respect to the start position as specified by the mode specified. Consider the following examples : Fh= open("Marks.txt", "r") le nl i i pe ds : : Of the fle defeat) mt 6 Fm the beginning fh, seek (30) will place the file pointer at 30th ahead seek(30) cure leone sion (mode = 1p io fh. seek(32, 1) wil place file pointer at 30 bytes behind (backward direction) é ‘Puseak(-20, 20a ae from end-of file (mode = 2) il place the fl pone at Sie " Fh. seek(30, 2) — ete oe -’ategs fiseek (-5. 1) Backward movement of file-pointer is not possible from the beginning of the file (BOF). You can move the file-pointer in forward direction (positive vaive ; rece or enement of file-pointer isnot possible from can Salt ill sa ths low consider some examples based on the above-discussed _negatve value for byes). % onbiaelumamans eiiamcnar meee 3 Check the position of file pointer after read( ) function fh= open("Marks.txt", "r") print(fh.read()) print("File-pointer is now at byte :", fh.tell()) Eatecpipst Produced by above code is : - 12,Hazel ,67.75 1S, Jiya,78.5 16,Noor 68.9 17, Akshar,78.9 (23, Jivin, 89.5 File-pointer is now at byte : 75 can make out that file has 74 characters including ‘\n’ at the end of every line and thus the entire file, the file-pointer is at the end-of file and thus showing 75 gM vale is retued by tell) 12 Read the last 15 bytes of the file “Marks.txt” ‘fh = open("Marks.txt", "r") Fh. seek(-15, 2) <———_ Place the fle pointer 15 bytes before the end of stri = fh.read(15) Pe ene a print("Last 15 bytes of file contain :", str1) Functions seek( ) and tell{ ) work Identically in text and binary files. wears, updation process mentioned earlier, which is cord to be updated by searching for it. in the loaded record in memory (the read record). the file at the exact location of old record COMPUTER SCIENCE WITH PYTHON To determine the exact location the enhanced version of the updation process would Pe (Open file in read as well as write mode. (Important) (i) Locate the record m ord (@) Firstly store the position of file pointer (say xpos) before OB bse conatis n (b) Read record from the file and search the key in it through aP (stored in ste 3 (©) If found, your desired record’s start position is available in FD sired a (ii) Make changes in the record by changing its values in memory, 2 €°S'°°" (ie) Write back onto the file at the exact location of old record. tion) using seek( ) (a) Place the file pointer at the stored record position (the exact Feed eins ie, at rpos, which was stored in step a (the exact location of the tea (®) Write the modified record now. The previous step is important = Frocttion So any operation read or write takes place at the current file poin is s P sition. So the file pointer must be at the beginning of the record to be over-writen Following example program illustrates this process. Write a program to update the records of the file Stu.dat so that those who have scored more | ) 5.17(a) Consider the binary file Stu.dat storing student details, which you created in earlier progr rogram 81.0, get additional bonus marks of 2. Note. Important statements have been highlighted. import pickle stu = {} found = False # open binary file in read and write mode fin = open(‘Stu.dat', "rb+') <———— is important to oper the file in read ‘as well as write mode ; hence rb+ #t declare enpty dictionary object to hold read records # read from the file try: Before reading any record, firstly store its beginning while True : pee aa, positon ~ its exact position rpos=fin.tell() # store file-pointer position before reading the record stu = pickle. load(Fin) Af stuf "Marks"] > gn ¢¢<—— amie te dei eco rough search condition placing the # changes made in the record; 2 bonus marks added Flepoiter ty : = the file-pointer at the exact location of the recor locaton ofthe # now write the updated record on the exact loca record you stored earlier found = True After placing the fle-pointer atthe exact location except EOFError: ow int te ndlted bee, if found == False: print("Sorry, no matching record found.") else: print("Record(s) successfully updated.) fin.close() # close file above program will look for the desired matching record (with marks » 81) and make the init and write back into the file, Record(s) successfully updated, program reads the modified file and displays its records. You can gee yourself if the is modified, '7(b) Display the records of file Stu.dat, which you modified in of program 5.17(a) Amport pickle stu ={} # declare empty dictionary object to hold read records # open binary file in read mode fin= open(‘Stu.dat', 'rb') # read from the File try: print("File Stu.dat stores these records") while True : stu pickle.load(fin) tread record in stu dictionary from fin print(stu) fiprint the read record ‘except EOFError: fin.close() # close file he output produced by above program is : dat stores these records ytharks': 85.5} ———— Se thematine record's marks have been a cieelinacket ified (compare with the output of Guneet', 'Marks': 80.5} ea *warks' -can also place the file pointer backwards using negative values in bytes BUT for that you the size of record (i.e, the object stored in the file) in bytes. Getting the size of a forward in Python, For that you need to import a different module dules here is beyond the scope of the book and thus you to use the method covered above. earlier ‘a program to modify the name of rolino 12 as Gurnam in file Stu.dat (created in # declare empty dictionary object to hold read records n(*Stu.dat", ‘rb+') #open binary file in read and write mode the file

You might also like