File Operations =============== What is file? -------------- => According to the programming requirements, when we want to store the data permanently we need to use "files". Types of files? ---------------- => two types of files: 1) Text files ==> must be with ".txt" extension ==> includes: the character data Ex: abc.txt name: designation: working location: 2) Binary files ==> these are used to store "binary data" => binary data includes with: images, audio, video information etc. Opening the file? ------------------ open(): ------- Syntax: file-object/file-pointer = open(fileName, mode) here: mode ==> the operation of the file File modes: ----------- 1) r-mode ---------- => read-mode => r-mode we can specify like: "r" or 'r' => when we want to open the existing file for reading operation we can use "r" mode. => Here, the file pointer is positioned at the beginning if the file. => If the specified file is not existed, we can get "FileNotFoundError". => r-mode is the default mode. 2) w-mode --------- => write mode => 'w' or "w" => when we want to open the existing file for writing the data we can use "w" mode. => If the specified file existing with some data, then this file will be overridden. => If the specified file is not existed then the file object/file pointer can create the new file with the given name. 3) a-mode --------- => append mode => 'a' or "a" => can use to open the existing file for the append operation. => a-mode never overridden the data when the specified file is existing with some data. => This can create the new file when the specified file is not existed. 4) r+-mode ----------- => read + write => the previous data in the file will not be deleted. => The file object is pointing to the beginning of the file. => 'r+' or "r+" 5) w+-mode ---------- => 'w+' or "w+" => write + read => It will override existing data. 6) a+-mode ----------- => 'a+' or "a+" => append + read => It won't override the data. 7) x-mode ---------- => 'x' or "x" => It can always use to create the new file for writing operation. => when we can try to open the existing file it can give "FileExistsError". => exclusive mode Ex: fp = open('abc.txt', 'r') Closing the file? ------------------ close(): -------- Syntax: file-object.close() About File Object? ------------------- => file object can be created while opening the file in any mode. => File object properties: 1) name 2) mode 3) closed 4) readable() 5) writable() Ex: ---- f = open("abc.txt", "w") print("Name of the file = ",f.name) print("Mode of the file = ",f.mode) print("Is file closed? = ",f.closed) print("Is readable = ",f.readable()) print("Is writable = ", f.writable()) Writing data into the text file -------------------------------- write(): -------- Syntax: file-object.write(string-data) (or) file-object.write(list-of-lines-of-data) Ex: --- f = open("abcd.txt", "w") f.write("Python is Easy to Learn") f.write("Python is using in Data science") f.write("Python is also useful in AI/ML") list_file_data = ["Rajesh\n", "Rakesh\n", "Neelam\n", "Sonu\n"] f.write(list_file_data) f.close() Reading data from the text file -------------------------------- => reading the data from the file: 1) read() => to read the total data of the file 2) read(n) => to read only the n-characters of the file 3) readline() => to read only one line 4) readlines() => to read all lines of a file. Ex: ---- f = open("abc.txt", "r") data = f.read() print(data) data1 = f.read(20) print(data1) line = f.readline() line1 = f.readline() print(line) lines = f.readlines() print(lines) With statement --------------- Ex: ---- with open("abc.txt", "w") as f: f.write("Python\n") f.write("Java\n") f.write(".Net\n") print(f.closed) # False print(f.closed) # True seek() method -------------- => which can be used to move the cursor to the specified position. Ex: ---- data = "All students are Intelligent" f = open("abc.txt", "w") f.write(data) with open("abc.txt", "r+") as f: text = f.read() print(text) Print("The current position = ",f.tell()) f.seek(10) Print("The current position = ",f.tell()) f.write("xyzxyz") f.seek(0) text = f.read() print(text) tell() method -------------- => which can be used to know the current position of the cursor or file pointer we can use "tell() function". Ex: ---- f = open("abc.txt", "r") print(f.tell()) print(f.read(2)) print(f.tell()) print(f.read(4)) print(f.tell()) Handling of Binary data ------------------------ rb, wb, ab, r+b, w+b, a+b, xb Handling of csv files ---------------------- csv ==> comma separated values Writing the data into csv files: -------------------------------- import csv with open("emp.csv", "w", newline = '') as f: w = csv.writer(f) w.writerow("ENO", "ENAME", "ESAL", "EADDR") n = int(input("Enter number of employees:") for i in range(n): eno = input("Enter employee ID:") ename = input("Enter employee Name:") esal = input("Enter employee salary:") eaddr = input("Enter employee address:") w.writerow(eno, ename, esal, eaddr) print("Total Employee data written into csv file successfully")