File Object Properties: ======================= # File object and its properties fp = open("abc.txt","w+") print("File Name = ",fp.name) print("The Mode of the file = ",fp.mode) print("is file writeable?:",fp.writable()) print("is file readable?:",fp.readable()) print("is file closed?:",fp.closed) fp.close() print("is file closed?:",fp.closed) =================================================== How to write the data into file: ================================ write() ======= ==> can allow to write the string (one line data). Syntax: file-object.write("data") writelines() ============ ==> can allow to write the data in more lines. Syntax: file-object.writelines(data) # write operations fp = open("abc.txt","w") data = ["abc\n","def\n","ghi\n"] fp.write("Hi\n") # fp.write("Good Morning.\n") # fp.write("Welcome To Ashok IT.") fp.writelines("Hi") fp.writelines("Good Morning.") fp.writelines("Welcome to Ashok IT.") fp.write(data) fp.writelines(data) ============================================================ Reading the data from the file: =============================== read(): ====== ==> use to read the total data from the file. Syntax: file-object.read() file-object.read(n) # to read first 'n' characters of the file readline() =========== ==> to read only one line of data from file. Syntax: file-object.readline() readlines(): ============ ==> can read all the lines of data in list format. Syntax: fp.readlines() fp = open("abcd.txt","r+") # writing the data # data = ["Python\n", "Java\n", "C++\n", "C\n", "C#\n", "JavaScript"] # # fp.writelines(data) # fd = fp.read() # fd1 = fp.read(10) print("The File data is = ") # print(fd) # print(fd1) print(fp.readline()) # print(fp.readlines()) line = fp.readlines() for i in line: print(i) ================================================ with Statement: =============== ==> can be used to define block of operations on the file using file pointer. ==> after all the operations, the with statement can close the file automatically (without writing close() explicitly.) with open("abc.txt","w+") as fp: fp.write("Hi\n") fp.write("Good Morning.\n") fp.write("Welcome To Ashok IT.") print("THe Data = ",fp.read()) print("Is file closed:",fp.closed) print("Is file closed:",fp.closed) ========================================================= tell(): ======= ==> to know the current position of the file pointer, we can use "tell()". Syntax: fp.tell() fp = open("abc.txt","r") print(fp.tell()) print(fp.read(3)) print(fp.tell()) print(fp.read(6)) print(fp.tell()) ========================================= seek(): ====== ==> can be used to move the file pointer from current position to any specified position. Syntax: fp.seek(offset, fromwhere) offset ==> number of characters fromwhere ==> three values 0 ==> from the beginning ==> python supports this only 1 ==> from current position 2 ==> from ending of the file fp.seek(offset, 0) fp.seek(offset) fp = open("abc.txt","r") print(fp.tell()) print(fp.read(3)) print(fp.tell()) print(fp.read(6)) print(fp.tell()) fp.seek(15) print(fp.tell()) fp.seek(30) print(fp.tell())