FILE HANDLING Ajay Godwal PGT CS , KV Ballia Need of File If something is to be stored in our computer in permanent form then we create a file in suitable format(jpeg,png,mp3,txt,doc) and this files is stored in any secondary storage device. File Handling It is collection of data, stored on any secondary storage device We need data to be written, modified, deleted and read from a physical storage like a disk by using a programming language. This is quite helpful in storing data for future use. Python too provides several features to perform various operations on disk files using built-in functions. Types of Files Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n’) in python by default. Binary files: In this type of file, there is no terminator for a line and the data is stored after converting it into machine understandable binary language. CSV A comma-separated values 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. The use of the comma as a field separator is the source of the name for this file format. nm=input (“Enter Your Text”) User Input Output print(nm) nm=“we are learning file handling” Primary Storage RAM input() file write() Primary Storage RAM User Input Output Print() var Read() myFile …………… ……………. Secondary Storage Ex. HardDisc Text File in Python To open a file in different modes file=open(filename,mode) r w Opens a file for reading only . The file pointer is placed at the beginning of the file. Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, it creates a new file for reading and writing. r+ Opens a file for both reading and writing. a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.. x open for exclusive creation, failing if the file already exists NOTE rb,rb+,wb,wb+,ab+ are other modes related with binary files Writing/append into file Open a file file=open(“myfile.txt”,”w”) Take User Input str=input(“Enter Your Text”) Write into file Demo Prg in Jupyter file.write(str) Use mode “a” to append more Close the file file.close() Reading From File Open a file file=open(“myfile.txt”,”r”) Read From File str=file.read() Print on Screen print(str) Close the file file.close() Demo Prg in Jupyter read(n)-to read n char from a file supermom.txt Super Mom Mom, you're a wonderful mother, So gentle, yet so strong. The many ways you show you care Always make me feel I belong. You're patient when I'm foolish; You give guidance when I ask; It seems you can do most anything; You're the master of every task. readline() –to read a line at at time supermom.txt Super Mom Mom, you're a wonderful mother, So gentle, yet so strong. The many ways you show you care Always make me feel I belong. You're patient when I'm foolish; You give guidance when I ask; It seems you can do most anything; You're the master of every task. readlines()-stores in list of string write() & writelines() write() writes a string str to the file writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings flush() flush()-write() writes a string str to the fileDue to buffering, the string may not actually show up in the file until the flush() or close() method is called.it forces the writing of data on disc still pending in output buffer User Input Output buffer file flush() Write() Primary Storage RAM file.write(“one”) file.write(“two”) file.flush() file.write(“three”) file.write(“four”) File.close() myFile …………… ……………. Secondary Storage Ex. HardDisc seek() & tell() seek() to set position of file pointer at a specific byte no tell() to access position of file pointer Practice Qus-2/3 Marks Practice Qus-2/3 Marks Thank You