File Handling: ============== close the file: =============== close(): ======= Syntax: file-pointer.close() Properties of File object/file pointer: ======================================= 1) getting of file name ======================== name: (property) ===== file-object.name 2) getting of the file mode: ============================ mode: (property) ===== file-object.mode 3) check whether the file is readable or not? ============================================= readable() =========== Syntax: file-object.readable() ==> True/False 4) check whether the file is writeable or not? ============================================== writeable() =========== file-object.writeable() ==> True/False 5) is file closed? ================== closed: (property) ====== file-pointer.closed ==> True/False # file Object Properties fp = open("ravi.txt","r") print("Name of the file = ",fp.name) print("Mode of the file = ",fp.mode) print("Is file readable? = ",fp.readable()) print("Is file writeable? = ",fp.writable()) print("Is file closed? = ",fp.closed) # closing the file fp.close() print("Is file closed? = ",fp.closed) =============================================================== File Operations: ================ Writing the data into files: ============================ 1) write(): ========== ==> we can write string of data into a file (within the line). Syntax: file-object.write(string-data) # writing operations # fp = open("ravi.txt","w") # fp1 = open("ravi.txt","w+") # fp = open("a.txt","x") # fp = open("a.txt","r") fp = open("a.txt","a") """ to write data into file: we can use: "w-mode", "w+-mode", "r+-mode", "a-mode", "a+-mode","x-mode" """ # writing of data into file when it is in w-mode # fp.write("Python\t") # fp.write("Object\t") # fp.write("Oriented\t") # fp.write("Programming\t") # fp.write("Language") fp.write("Hello") # closing the file fp.close() ======================= writelines() ============ ==> is used to write the data into multiple lines of the file. Syntax: file-object.writelines(list of lines of content) # writing operation using writelines() fp = open("c.txt","x") ld = ["Python\n","Object\n","Oriented\n","Programming\n","Language\n"] # fp.write(ld) # fp.write(ld[0]) fp.writelines(ld) fp.close() ============================================= Reading of data from the file: ============================== four possible ways: 1) read() 2) read(n) 3) readline() 4) readlines() 1) read(): ========= ==> used to read the total data from the file. Syntax: file-pointer.read() # reading with read() fp = open("ravi.txt","r") fp1 = open("a.txt","r+") fp2 = open("c.txt","a+") data = fp.read() data1 = fp1.read() # data2 = fp2.read() print(data) print(data1) # print(data2) fp.close() fp1.close() # fp2.close() =================================== 2) read(n): =========== ==> used to read the data from the file upto 'n' characters. Syntax: file-pointer.read(n) fp = open("c.txt","r") # data = fp.read(10) data = fp.read(20) print(data) fp.close() ======================================== 3) readline(): ============== used to read the data of only one line at a time. Syntax: file-object.realine() fp = open("c.txt","r") data = fp.read(10) data = fp.read(20) data1 = fp.readline() data2 = fp.readline() print(data) print(data1) print(data2) for i in range(5): fp.readline() fp.close() ======================================= readlines() ========== ==> to read all the lines of file ==> we can use "readlines()". Syntax: file-pointer.readlines() fp = open("c.txt","r") data = fp.readlines() print(data) for i in data: print(i) ========================================== with statement: =============== ==> with is the keyword ==> while opening the file, with is used. to define the block of operations on the file. Syntax: with open("file.txt","mode") as file-object: block of operations # with statement with open("c.txt","a") as fp: fp.write("Hi\n") fp.write("Hello\n") fp.write("Good Morning.") print("Is file closed? = ",fp.closed) print("Is file readable? = ",fp.readable()) fp.close() print("Is file closed? = ",fp.closed)