File Handling: ============== for programming purpose, files can be considered for the storage permanently. ==> files are classified into two types: 1) Text Files/Character file ==> .txt 2) Binary Files Note: ===== Every file must be defined with some extension. Ex: image files ==> .jpg/.jpeg/.png etc. audio files ==> .mp3/.ong video files ==> .3gpp/.mp4/.ong etc. How to Open the files: ====================== about Text files: ================= open(): ====== ==> a built-in function, which we can use to open the existing file or to create the new file. Syntax: file-object-name/file-pointer = open(file-with-extension, mode-of-the-file-in-quotes); Here: file-with-extension ==> file name mode-of-the-file: write mode ==> w ================= # write mode # w-mode can allow to open the existing file or # also allow to create the file if the specified file is not existed. # w-mode can write the data if the existing file is already with some data means: that old # data can be vanished and re-write the new data from the beginning. # opening the existing file in w-mode fp = open("file1.txt","w") # opening the new file in w-mode fp1 = open("file2.txt","w") ====================================== read mode ==> r ================ r-mode (read mode) # r-mode can help to open the existing file only # it cannot be able to create the new file # opening the existing file # r-mode never vanish the old data from the existing file. fp = open("file1.txt","r") # opening the new file in r-mode # error: File Not Found Error fp1 = open("file3.txt","r") ================================================== append mode ==> a ================== # a-mode ==> append mode # append can be able to add the data to existing data # ld = [1,3,5,7,9] # # print(ld) # # ld.append(15) # # print(ld) # a-mode can be able to open the existing file without vanishing the existing data fp1 = open("file1.txt","a") # a-mode can also able to create the new file ======================================================== write-read mode ==> w+ ====================== # w+-mode ==> write + read # w+-mode can be able to open the existing file as like w-mode by vanishing the old content fp1 = open("file1.txt","w+") fp1.write("Hi all. Good Evening.") print(fp1.read()) ============================================================ read-write mode ==> r+ ====================== # r+-mode ==> read+write # like r-mode: r+-mode also can be able to open the existing file only fp1 = open("file1.txt","r+") data = fp1.read() print(data) fp1.write("Welcome to Ashok IT.") ====================================================== append-read mode ==> a+ ======================= # a+-mode ==> append + write # like a-mode,a+-mode can be able to open the existing file and new file fp1 = open("file1.txt","a+") fp2 = open("file5.txt","a+") data2 = fp1.read() print(data2) fp1.write("\nWe are from Hyderabad") # data3 = fp1.read() print(data3) ========================================== exclusive mode ==> x ==================== # x-mode ==> Exclusive mode # same as w-mode # x-mode can always open the file for writing the data only in new file always. # if file is already existed ==> file exist error # fp1 = open("file1.txt","x") # fp1 = open("file4.txt","x") # fp1.read() # fp1 = open("file6.txt","x") # fp1.write("Hello All.") # d = fp1.read() # print(d) fp1 = open("file7.txt","x") fp1.write("Welcome to My Python Class.") # d = fp1.read() ==> not possible with reading # print(d) ==================================================== File object properties: ======================= fp1 = open("file1.txt","r") # some properties print("Name of the file = ",fp1.name) print("Mode of the file = ",fp1.mode) print("is readable? = ",fp1.readable()) print("is writeable? = ",fp1.writable()) # when we have opened the file for handling. # after the file operations we should close the file extensively using close() # if you cannot be close the file extensively, the PVM can close this automatically. print("Is file closed? = ",fp1.closed) fp1.close() print("Is file closed? = ",fp1.closed