FILE HANDLING IN PYTHON NEED FOR FILE HANDLING 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.Mostly, in programming languages, all the values or data are stored in some variables which are volatile in nature. Because data will be stored into those variables during run-time only and will be lost once the program execution is completed. Hence it is better to save these data permanently using files. 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. 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 Python, File Handling consists of following three steps: 1. O pen the file. 2. P rocess file i.e perform read or write operation. 3. C lose the file. Types of File : 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. 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. CSV Files: CSV stands for Comma Separated Values. CSV is just like a text file, in a human readable format which is extensively used to store tabular data, in a spreadsheet or database. The separator character of CSV files is called a delimiter. Default delimiter is comma (,). Other delimiters are tab (\t), colon (:), pipe (|), semicolon (;) characters. STEPS TO PROCESS A FILE 1. Determine the type of file usage. i. Reading purpose : If the data is to be brought in from a file to memory. ii. Writing purpose : If the data is to be sent from memory to file. 2. Open the file and assign its reference to a file object or file-handle. 3. Process the file as required : Perform the desired operation from the file. 4. Close the file. OPENING A TEXT FILE Before any reading or writing operation of any file,it must be opened first of all.Python provides built in function open() for it. It accepts two parameters : filename, and mode. Syntax: <file_object_name> = open(<file_name>,<mode>) Example: f = open(“demo.txt”,”r”) File Objects: o It serves as a link to file residing in your computer. It is a reference to the file on the disk and it is through this link python program can perform operations on the files. o File access modes: o It governs the type of operations (such as read or write or append) possible in the opened file. Mode Description “r” Read Default value. Opens a file for reading, error if the file does not exist. “w” Write Opens a file for writing, creates the file if it does not exist “a” Append Opens a file for appending, creates the file if it does not exist “r+” Read and Write File must exist otherwise error is raised.Both reading and writing operations can take place. “w+” Write and Read File is created if it does not exist.If the file exists past data is lost (truncated).Both reading and writing operations can take place. “a+” Append and Read File is created if it does not exist.If the file exists past data is lost (truncated).Both reading and writing operations can take place. CLOSING A FILE close(): Used to close an opened file. After using this method, an opened file will be closed and a closed file cannot be read or written any more. It is important to close your files, as in some cases, due to buffering, changes made to a file may not show until you close the file. Syntax: <fileObject>.close() Example : f.close() READING AND WRITING: A Program reads a text/binary file from hard disk. Here File acts like an input to the program. Followings are the methods to read a data from the file: 1. read() METHOD 2. readline() METHOD 3. readlines() METHOD read() METHOD By default the read() method returns the whole text, but you can also specify how many characters you want to return by passing the size as argument. Syntax: <file_object>.read([n]) where n is the size To read entire file : <file_object>.read() To reads only a part of the File : <file_object>.read(size) f = open("demo.txt", "r") print(f.read(15)) "demo.txt". # Returns the 15 first characters of the file readline() METHOD readline() will return a line read, as a string from the file. Syntax: <file_object>.readline() Example f = open("demo.txt", "r") print(f.readline()) #This program will return the first line in the file “demo.txt” irrespective of #number of lines in the text file. readlines() METHOD readlines() method will return a list of strings, each separated by \n. readlines() can be used to read the entire content of the file. Syntax: <file_object>.readlines() It returns a list, which can then be used for manipulation. Example : f = open("demofile.txt", "r") print(f.readlines()) Difference between read(), readline() and readlines(): The read() reads from a file in read mode, and stores its contents in a string type variable. It can either read the whole content from the file if a parameter is not passed and reads only as many characters as the size passed to the read(). The readline() function reads from a file in read mode and returns the next line in the file or a blank string if there are no more lines. The return data type is of string type. The readlines() function, also reads from a file in read mode and returns a list of all lines in the file. The returned data is of list type. WRITING TO A TEXT FILE A Program writes into a text/binary file in hard disk.Followings are the methods to write a data to the file: o write () METHOD o writelines() METHOD To write to an existing file, you must add a parameter to the open() Function which specifies the mode : o "a" - Append - will append to the end of the file o "w" - Write - will overwrite any existing content APPEND AND WRITE: If you open a file in in “w” or write mode , Python overwrites an existing file or creates a non- existing file. For an existing file with the same name , the earlier data gets lost. If you want to write into a file while retaining the old data , the file must be opened in append or “a” mode. write() Method write() method takes a string ( as parameter ) and writes it in the file. For storing data with end of line character, you will have to add \n character to end of the string. Example: Open the file "demo_append.txt" and append content to the file: f = open("demo_write.txt", "a") f.write("Hello students \n We are learning data file handling…..!") f.close() f = open("demo_write.txt", "r") #open and read the file after the appending print(f.read()) writelines() METHOD For writing a string at a time, we use write() method, it can't be used for writing a list, tuple etc. into a file. Python file method writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings.So, whenever we have to write a sequence of string / data type, we will use writelines(), instead of write(). # Program to demostrate writelines() f = open("demofile.txt", "a") f.writelines(["We love python!", "\nFile Handling"]) f.close() #open and read the file after the appending: f = open("demofile.txt", "r") print(f.read()) The flush() function: When you write on to a file using any of the write functions, Python holds everything to write in the file and pushes it onto actual file on storage device at a later time. So to force Python t write the contents onto files you can use flush(). v The flush() function forces the writing of data on disc still pending in the output buffer. Syntax : <file_object>.flush() # Program to demostrate flush() f = open("demo_flush.txt","w+") f.write("India is my country.\n") f.flush() # After some statements x = "Jai Hind" f.write(x) f.close() BINARY FILES: Files that store objects as some byte stream are called binary files. That is, all the binary files are encoded in binary format , as a sequence of bytes, which is understood by a computer or machine. In binary files, there is no delimiter to end the line. Since they are directly in the form of binary, there is no need to translate them. But they are not in human readable form and hence difficult to understand. Python objects (list, dictionary etc) have a specific structure which must be maintained while storing or accessing them. Python provides a special module called pickle module for this. PICKLING refers to the process of converting the structure to a byte stream before writing to a file. The process to converts any kind of python objects (list, dict, etc.) into byte streams (0s and 1s) . UNPICKLING is used to convert the byte stream back to the original structure while reading the contents of the file. PICKLE Module Before reading or writing to a file, we have to import the pickle module. import pickle It provides two main methods : dump() method and load() method. pickle.dump() method is used to write the object in file which is opened in binary access mode. Syntax : pickle.dump(<structure>,<FileObject>) Here Structure can be any sequence in Python such as list, dictionary etc. FileObject is the file handle of file in which we have to write. # Simple program to write to binary file import pickle f = open("binary_file1.dat","wb") city = ["Banglore","Delhi","Hyderabad"] pickle.dump(city,f) f.close() pickle.load() Method pickle.load() method is used to read data from a file Syntax : <structure> = pickle.load(<FileObject>) Structure can be any sequence in Python such as list, dictionary etc. FileObject is the file handle of file in which we have to write. # Program to read data from a binary file import pickle f=open("binary_file1.dat","rb") lst = pickle.load(f) print("Content in binary file is : ") print(lst) f.close() CSV FILES : CSV stands for Comma Separated Values. It is a type of plain text file that uses specific structuring to arrange tabular data such as a spreadsheet or database. CSV like a text file , is in a human readable format and extensively used to store tabular data, in a spreadsheet or database. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The separator character of CSV files is called a delimiter. Default delimiter is (,). Other delimiters are tab(\t), colon (:), pipe(|), semicolon (;) characters. READ FROM A CSV FILE To read data from csv files, reader function of csv module is used. csv.reader() returns a reader object.It loads data from a csv file into an iterable after parsing delimited data. STEPS TO READ 1. Import csv module import csv 2. O pen csv file in read mode. f = open(“csv_demo.csv”,”r”) 3. C reate the reader object. demo_reader = csv.reader(f) 4. F etch data through for loop, row by row. 5. C lose the file f.close() # Program to read from a csv file import csv f = open("emp.csv","r") emp_reader=csv.reader(f) for row in emp_reader: print(row[0],row[1],row[2]) # print(row) : As a list f.close() WRITING IN TO CSV FILES: To write data into csv files, writer() function of csv module is used. csv.writer(): o Returns a writer object which writes data into writer object. Significance of writer object The csv.writer() returns a writer object that converts the data into a delimited string.The string can be later converted into csv files using the writerow() or writerows() method. Syntax: <writer_object>.writerow() : Writes one row of data in to the writer object. <writer_object>.writerows() Writes multiple rows into the writer object. # Program to write into a CSV File import csv f=open("emp.csv","w",newline="") emp_writer = csv.writer(f) emp_writer.writerow(["EmpName","EmpID","Dept"]) # writerow() is used to write a single row emp_rec = [] while True: print("Enter Employee details: ") empname = input("EmpName : ") eid = int(input("EmpID : ")) dept = input("Department : ") emp_rec.append([empname,eid,dept]) ch = input("Do you want to continue ?? (Y?N)") if ch == "N" or ch =="n": break emp_writer.writerows(emp_rec) ##for i in emp_rec: ## "" Alternate method using writerows emp_writer.writerow(i) ************** THE END *************
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )