FILE HANDLING PART-02: ====================== HOW TO WRITE THE DATA INTO FILE? ================================ ==> two functions: 1) write() 2) writelines() 1) write(): =========== to write something into file, we should open the file with: w-mode/w+-mode/r+-mode/a-mode/a+-mode/x-mode Syntax: file-object.write(data) # write() function can write data in the same line in a file. fp = open("abc1.txt","w") # ld = ["Python","is","High-level","Programming","Language"] # writing the data into abc1.txt file fp.write("Hi\n") fp.write("Good Evening\n") fp.write("We are from Ashok IT.\n") fp.write("Ashok IT is located in Hyderabad.\n") # fp.write(ld) =========================================== writelines(): ============= Syntax: file-object.writelines(list of string content) fp = open("abc2.txt","a") ld = ["Python is \n","Excellent Language\n","To Learn Easily\n"] fp.writelines("Hi\n") fp.writelines("Welcome To Ashok IT.\n") fp.writelines("We are from Hyderabad.\n") fp.writelines(ld) =================================================== Inline functions and block functions: ===================================== Inline functions ==> can always do the task within the same line (side by side in a line) Ex: write(), writelines() block functions ==> can always take the new line with every definition Ex: print() ============================================== How to read the data from text files? ===================================== ==> to read data from files: we should open the file in: r-mode/r+-mode/w+-mode ==> four different ways: 1) read() 2) read(n) 3) readline() 4) readlines() # reading the data: # read() ==> can read whole data from file character by character. # read(n) ==> can help to read the data upto n-number of characters # readline() ==> can read the data of only one line at a time. # readlines() ==> can read all the lines of the content/data from the file fp = open("abc1.txt","r") # fileData = fp.read() # fileData1 = fp.read(15) # want to read upto 15-characters # print(fileData) # print() # print(fileData1) # d1 = fp.readline() # d2 = fp.readline() # d3 = fp.readline() # d4 = fp.readline() # # print(d1) # print(d2) # print(d3) # print(d4) linesOfData = fp.readlines() for i in linesOfData: print(i) =================================== with statement: =============== ==> can help to define block of file actions. ==> with ===> keyword # with statement with open("abc3.txt","w") as fp: fp.write("Python\n") fp.write("Java\n") fp.write("JavaScript\n") fp.write("Node JS\n") print("The File Name = ",fp.name) print("Mode of the file = ",fp.mode) print("Allowed for writing? = ",fp.writable()) print("Allowed for reading? = ",fp.readable()) print("Is file closed? = ",fp.closed) print("Is file closed? = ",fp.closed) ======================================================= tell(): ====== ==> a built-in function can return the current position of the file pointer. Ex: "abc.txt" Hi Hello Good Evening This is our File Handling Concept. tell() ==> 0 read(10) tell() ==> 11 read(20) tell() ==> 32 Syntax: file-pointer/file-object.tell() fp = open("abc1.txt","r") print("The Current Position of the fp = ",fp.tell()) fp.read(5) print("The Current Position of the fp = ",fp.tell()) fp.read(15) print("The Current Position of the fp = ",fp.tell()) ============================================= seek(): ======= abc.txt Hi Hello Welcome To Ashok IT. tell() ==> 0 ==> seek() is a built-in function can use to move the file pointer from one position to another position within the same file. Syntax: file-pointer.seek(offset, fromWhere) file-pointer.seek(offset, 0) file-pointer.seek(offset) here: offset ==> number of positions fromWhere ==> Python-3: fromWhere ==> default value ==> 0 ==> from the beginning of the file Python-2: fromWhere: 0 ==> from beginning 1 ==> from current position 2 ==> from end of file fp = open("abc1.txt","r") print("The Current Position of the fp = ",fp.tell()) fp.seek(10,0) print("The Current Position of the fp = ",fp.tell()) fp.seek(20) print("The Current Position of the fp = ",fp.tell()) fp.seek(10,0) print("The Current Position of the fp = ",fp.tell()) ======================================================== How to handle the binary files? =============================== fp1 = open("python.jpg","rb") fp2 = open("pythonCopy.jpg","wb") bytes = fp1.read() fp2.write(bytes) binary file modes: read ==> rb write ==> wb append ==> ab r+b w+b a+b xb