1.3. File Handling

You might also like

Download as pdf
Download as pdf
You are on page 1of 8
File Handling File 1/0 AFILE is a sequence of bytes or data stored on a permanent storage device (HD), which can be read, written or rewritten according to requirement. File 1/0 means transfer of data from secondary memory (hard disk) to main memory oF vice versa. =5 Types of File Python permits two types of files: Tex File Binary File characters exactly ina format in whieh memery Ne ASCI/UNICODE translation, 10 €OLcharacer 2 EOL (endo ine) character, len pytnon deta is . newine(\ data whic the users going to + Can be Regular tex fle, CSV fies, Apart from wextIrumerc binary au fies et get stored as open() - Opening a file open() function takes the name of the file as the first argument and mode of accessing file is the second argument It returns an object of file type using which file manipulation is done in the program. Syntax = open(file_name, access_mode) Access modes can be: + to read from the file (default) + w-to write into the file + a~to append (write at the end) data into the file Need of Data File Program executed till date run for a short period of, time and when they end data disappears. Need of a file is to store data permanently. In case of 1/0 operations input is done from these files and output is also written to the files. Steps for working with a text/data file: 1. Open the file for read/write 2. Perform required file operation 3. Close the file File Operations General file operations are: > Create Piraverse > Append/insert PSearch > delete > Modification > Creating copy | Opening and Closing Files ‘A Python program handles data file through- file variable/ file object/ file handle. ‘open() function opens a data file and attach it toa file object. The file object then access the file through various methods and functions available for file manipulation. Program Dita Fie File Opening Ex. f=open('Nature.txt’,'r') _ # open|'test.txt’) ‘The above command will open the text file Nature.txt in default reading mode and attaches it to file object f FileNotFoundError if no such file exists. It can be (absolute path): feopen("D:\\ABCD\ Nature") open("D:\ABCD\Natured.tst") Working with File When we work with file(s), a buffer (area in memory where data is temporarily stored before being written, to the file) is automatically associated with the file when we open it. flush() function ‘Te tush) method in Python fe handing clears the interna butfer of the file In Python, files are automatically flushed while closing them, However, a programmer can flush a fle before closing t by Using the fush() method. This method does not require any parameters andit does not return anything. syntax: .flush() While writing the content toa file first it goes to buffer, and once the buffers full, data is written to the fle Also, when the file is closed, any unsaved data is transferred to the file(). flush() function is used to force transfer of data from butfer to file. close() - closing a file File Mode (elnsbowc wesc) Flushes any unwritten information and closes the file | [wma | wale | boeteon Nome ane object. | te | ony [Mergen emer | See Python automatically closes a file when the [> Rie ie does ee, new fe reference object of a file is reassigned to another ww | xt | witeony [* Hike aesdy exits Python | Begoning file. eee ‘As a programmer it’s a good practice to close the file ‘Content willbe over writen explicitly. seond |" tenanedand sen dati |e orne v | em | “or” | SShoameentoroete | Eat File Mode (etme how the lew be aces) ‘partons an ae acs Reading from File Various method for reading from files are: ©) readline() ~ starting from cursor reads one line at a time including EOL character (default EOL is, newline character), and returns a string 4d) readline(n) ~ starting from cursor reads n number of character, but at most til end of a line, and returns a string etn empty sing sot Reading from File Various method for reading from files are: a) read() ~ reads data from the cursor till the end of the file and returns a string b) read{n) - starting from the cursor position reads n number of characters from the fie, if less than 1 characters it reads till the end of file and returns a string. urns emery sting oot Reading from File Various method for reading from files are: e) readlines() ~ from the cursor reads all the lines of, the file and returns the list of lines f)_readlines(n) — starting from the cursor position reads n number of characters from the file (reads the complete line where nth character is present), returns as a list of lines furs empry i eof File Pointer Ittells the current position in the file where writing or reading will take place. Its like cursor of Microsoft word. With reading or writing of every character it moves forward. > For reading mode it always starts from 0 byte postion. > For write mode aso it stats from byte position, as it truncates the file content. >» For append mode it starts from eof (end of file) position, as the content to add/append is atthe end of the fie leotact seen) owes thee ponterton (om bei) byt postion i le lect Teast cert poston fle potr na fle interme of ye Program to display the content ofa text fle ABCAXt ABC.txt oe feodet Fo w13 KL SN PQ RST eee #print(f.readt)) ficlosel) ftcode2 code feopen('A8C.txt’) feopent'abe:tet’) forxinf: “wtrue print, end=")#xs a line while x felose() Xetread() eee (nmr wnt ng print(X, end=") (Gover memory tation) felosel) Funct mn to display words where number of characters are less than four def DisplayWordl) 0 file=open('test.txt!,'r') line = file.read() word = line.split() for w in word if len(w)<4: print(w) c= file.close() print(c) DisplayWord\) [ Opening position of File Pointer ‘Opening position of file pointer retro, b+ | beginning ofthe file (0 byte) wwe, wb, w+ | beginning of the truncated fle (0 byte) atthe eof (end of file) ifan existing file 2, a, ab, abe else creates a new file and beginning offi (0 byte) [Program to count number of words ina text fle ABC.txt ‘AB COE Fo HID Heodet PQ RST Heode2 feopen('ABCtxt') — WW XYZ open(’ABC.tet') X-fread() fread) split() print(len(X.split)) print(len(X)) felose() Outpt: 10 feclose() codes print(len(opent'abc.tet’).read(.split()) def COUNTLINES(: file=open('Rivers.tst','r') lines = file.readlines() print(lines) count for w in lines: w=w.strip) print(w) if wf0] in "Aa: count=count+1 print(""\nTotal lines file,close() COUNTLINES() Function to count number of lines starting with A | Writing data to a File ‘Two methods: a) write() - writes string to the file attached with object pointer, for storing numeric values we have to convert it to string b)_writelines() — write() can’t be used for writing a list, tuple etc into a file. Sequence (of strings) data type including strings can be written using writelines() method in the file. Appending Data in a File Opens file for writing, and if it exists, then append data to the end of the file. Ifno file, new file will be created. Methods are: a) write() b) writelines() with statement Can be used for opening fle, with ensures that all the resources allocated to the file objects get deallocated automatically once we stop using the file, it ‘executes fclosel) automaticaly Not handling the exception “FlleNotFoundError’ Syntax with open{cfile mae, ) as fileObjet statements Brample \with open(RIVERS.txt, '") as & for line in f: print(line, end: Writing data to a File fopent X¥Z.te’, 'w/) fiwrite('Black White Orange’) fuwritestr((12, 34, 56))) fiwritelines( YELLOW’) fwritelines(('abe’, DEF) fwite('\n) ‘twwritelines('par’ xyz) fiwritelines('\n') fuwritelines(('ONE''1', TWO''2'), ‘writelines({'three’:3, four':4}) print(‘Data written successfully to XYZ.te’) felose() ‘black white Crangl12, 38, SIVELLOWabcDEF [ONeTWothresfour Append >>> feopen('newTEXT.txt’, 'a') >>> f.write("Programming’) ui >>> f.close(), >>> print(open(‘newTEXT.txt’).read|)) PythonProgramming >> >>> feopen('newTEXT.txt’, 'a') >>> fuwrtelines('abc', 1234’, xyz") >>> f.close() >>> print(open(‘newTEXT.txt').read()) PythonProgrammingabc1234xyz Handling FileNotFoundError try: pen("XYZ.txt') print(f.read()) f.close() except: print('File not found’) print("Program Executed") Handling FileNotFoundError firy: with open("RIVERSL.txt’, 'r') as for line in fi , ) lexcept print(line, end: print('No such file exist.") print('Program executed") Binary File Operations Serialisation/ Pickling Process of converting Python object hierarchy into byte stream, so that it can be written into a file. The byte stream when unpickled/ deserialized reconstruct the object with it structure. Deserialisation/ Unpickling It is the reverse process of pickling where a byte stream is converted back into object hierarchy and produces the exact replica of original object. pickle module does the above two. ALY FILE (wssisessions-rcones) feopen( Student dat; wb’) import pickle Dis{Adm'123, 'Name''Rashmi Stream'’Science’ pickle.dump[03, f) Print(Data written) )08':'20-08-2004'} >>> import pickle >>> fropen|Student.d >>> Depicke oad) >>> print(O} {Adm 123, 'Name':'Rashmi ‘Stream’ Science, DOB’ '20-09-2008) Binary File Operations write()/ writelines() writes a list/tuple/dictionary as 2 sequence of strings. But, we may need to write this content along with its structure in a file and then read also, with its structure, so as to handle the list/tuple/dictionary with its index/keys, Every object have some hierarchy associated. At times it is important that they are stored in a way so that their structure/hierarchy is maintained. This can be done by serialisation. Binary File Operations Two methods of pickle are: a) dump() ~ to write an object into a binary file syntax: pickle.dump(object, fileObject/handle) b) load{) ~to read an object from a binary file syntax: object=pickle.loadfileObject/handle) creating a copy of an image (image is neither list/ tuple/dictionary nor sequence of ASCII characters) pen(Naturet jpg, fb) open Nature2 pe, we) for chin f ‘ritech) ‘H=open( Nature jpg, 8) ‘frdose() ‘2-opent Nature jp, wh) ‘f2dosel) XefL.readl) print("Image copied") fawritaQX) ‘2.wrte(h.read)) flcosel) f2cosel) print("image copied") Relative and Absolute Path Files are organised into directories/folders. Every running program has a current directory and itis the default directory for most of the operations. While opening a data file, languages look for the data file in the current directory. The Osiepeatngssten) module provides functions for working with files and directories. os.getewdl) ~ returns the name of the current working directory Standard File Streams To work with data file, file object is required. Similarly, YO ‘operations from I/O devices are done through /0 stream object. Python performs i/0 operations through input), eval) and print) statement, so not required to explicitly use the /0 stream objects The standard streams available in Python are: i) Standard input stream i) Standard output stream i) Standard error stream ‘The above streams are nothing but file objects, which gets ‘automatically connected to /0 devices when we start Python. File Pointer It tells the current postion in the file where writing or reading will take place. Its like cursor of Microsoft word. With reading or writing of every ‘character moves forward, > For reading mode it always starts from 0 byte position > For write mode aso it stars from O byte postin, asi truncates the fle > Fer append mode it starts from eof (end of le) poston asthe cantent toade/append is atthe end ofthe ile oobet sce) Moves the te porter ton (tom benrin) bt postonin headers new Feo. els the rer poston fe pomterin te nts of te Relative and Absolute Path Path — The string that identifies the location ofa file the path of the file its lke address of the fe. Relative Path ~ Path starts from the current directory feopen("test.tx") searches the fie in current directory Absolute Path — Path that starts from the topmost directory in the file system fropen('EA\\temp\\test.txt') xesouns sunscreen) will open the above file from € drive for reading fropen(E\temp\testtnt) rerio rmtng Standard File Streams ‘To work explicitly with standard I/O stream, need to import sys module. Functions available are wrte() and read|). ‘The three standard streams are: i) sys.stdin() ~reads from standard input device ii)” sys.stdoutt) - writes to standard output device ii) sys.stderr()— error messages are written toit >>> import sys >>> feopen((TEXT.txt') >>> x=fread() >>> sys.stdout.write(x) ABABCPQR12345 abed5678pqrs Data written, successfullyS3 Moving File Pointer tell): Tells the current position of file pointer ina file in terms of byte. simtax: .tell() Moving File Pointer seek() Moves the file pointer to specified byte position in a file and returns the new position Syntax: .seek(offset [, mode]) Offset > is a number specifying number of bytes Mode > mode is a number either 0/1/2 (0 beginning of le (default), ost should be O/postive 1 current postion of file pointer offset may be negative 2 endof le offsets usually negative mode specties mavement af the file ponte vir the potion ofthe ile pointer as specified through 0/1/2 CSV file is a delimited text file that uses a comma to | separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas CSV (comma separated values) used to store tabular data in plain text format where the column names and row values are separated using commas. fant saith, Female 2 [Doe Jones, Hele 37 stherine’ Donovan rows representing stored values Moving File Pointer fseek/30,0) | Places the file pointer at 30 byte position from the beginning of file fseek(30) | Places the file pointer at 30 byte position from the beginning of file fseek(30, 1) | Moves the file pointer at 30 bytes ahead from the current position fseak(-30, 1) | Moves the fle pointer at 30 bytes back rom the eurrent position Taeek(30, 2) | Moves the fle pointer at 20 bytes back from the end of file fseek(-10, 0) _| Error: Invalid argument feeek(20,2) | Moves 20 bytes from eof (in memory] CSV files } The separator characteris called a delimiter > commas the default delimiter > Other delimiters can be: tab (\t), pipe(|), colon (:) and semi-colon (;) Properly parsing a CSV file requires the knowledge of exact delimiter used in a file Itisa text file, CSV files are very easy to work through programs CSV module of Python provides functions to work with csv files v vvy a] a C——-—- ‘Where Do SV Files Come From? (SV les are normaly created by programs that handle large amounts of databases aswell as import or use iin other programs For example, you might expon the resus of a data mining program to 8 CSV fle and then import that into a spreadsheet to analyse the data, {generate graphs for @ presentation, or prepare a report for publication. csv module SV Functions cswreader > function for reading records from csv files > function for writing records into csv files Opening and Closing CSV files file = open('Student.csv’, '') The command will open the csv file Student.csv through the file handle file file.close() The command will close the linked file. Writing into CSV file involves the conversion of user data into. \ritable delimited form and then writing that into a esv file The three functions for the task ae’ xvonrter) Teluins a witer object responsible for Converting the user's data into delimited Steng for wrtng inc the ev le obect ewrteroblec>.writerow) | writes one row of dta/one record onto the writer object. ‘ewrterbject>.writerownl) | writes multiple rows of ata/ multiple records onta the writer object. eter te et | Reading from CSV Files Reading from a csv file involves loading of a csv file's data, parsing (i. removing its delimiters), loading it into a python iterable. csv.reader() loads data from the CSV file , parses it and returns an iterable object, where each iteration returns a row of the CSV file. ‘The reader function is designed to take each line of the file and make a list of all values/columns (as strings). So each row from the CSV is alist of strings, where strings are the column/value of the CSV file. Questions Function to count number of words. Function to count uppercase, lowercase and digits, Function to create a list of all the <=3 letter words. Function to count number of me and my. Function to create a dictionary of vowel count (for AEIOU) Function to create a list ofall the words ending with a vowel Questions os. _Atext file named MESSAGE TXT contain some test. Another te fe hamed SMSTXT needs to be created such that it would store only the first 350 characters from the fle MESSAGEXT, Wiite a user-defines function LongToshor() in Python that woul perform the above task of creating SMSIXT from the already ensting file MESSAGE TXT. ‘text fle named CONTENTS-TXTcontalns some text, Write a user defined function Long¥ords) in Python which esplays all such ‘words ofthe fle whose lengths more than 9 alphabets. For example ifthe fie CONTENTSTAT contains "Conditional statements of Python programming language ar it only ‘Then the function LngWords|) should display the output as Coneitional statements programming CSV files with lineterminator v Aline terminator is a string used to terminate lines produced by writer objects. The default value is \"\n. \ris carriage return for Macintosh \nisine feed for Unix \r\n carriage return\line feed for Windows, Windows recognizes both vvy Therefore, default \r\n inserts one more line after every record, specification of line terminator as \n or \r only discards extra empty line in esv file. Questions Function to count number of lines. . WAF that appends the content of one file at the end of another file, Read both file names from the user. WAF to create a copy of an existing text file by capitalizing first letter of every sentence. . Write a method to write multiple lines of text contents into a text file daynote.txt file. Write a user defined function that displays the number of lines ending with 'H' in the file para.txt Questions asin. Wet a metoa/uneion STOUPCOUNT) in python to readcntets tom ate Forevamele: the content ofthe tes Themethe/fneton shes spy Wee ametoa/uneion ADP prthon toed eso 3s te WRITER ea ly those ines hh are tai eter satig th Aor Ferenc the contr of the eis The method shoud slay

You might also like