ASSIGNMENT FILE HANDLING (TEXT FILES) 1. What is a File? Why do we need to store data in files? Define the various types of files with an example of each. 2. What is the difference between Text Files and Binary Files? 3. Write the code in python to open a file named “try.txt” 4. f = open("data.txt") The above file will open in _______________ mode. 5. f = open("data.txt", "w") The above statement will return any error, if the file "data.txt" does not exist? 6. What error is returned by the following code, if the file does not exist. f=open("data.txt", "r") 7. Is there any difference between the following statements? f = open("data.txt") and f = open("data.txt", "r") 8. Write the code to open a file "name.txt" which is stored in folder named "demo" in C drive. 9. What is the difference between the write and append mode? 10. Where the file pointer present when we open file in the following mode 1. Read mode 2. Write mode 3. Append mode 11. Write a function stats() that accepts a file name and returns the file’s longest line. 12. Write the symbols used in text file mode for the following operations. • Read Only • Write only • Read and Write • Write and Read 13. What is the difference between read() and read(n) functions? 14. What is the difference between readline() and readlines() ? 15. Write a program to read first 10 characters from a file named “data.txt” 16. Write a program to read entire content from the file named “test.txt” 17. Write the output of the following, if the data stored in file “try.txt” is given below. (“f” is the file object associated with file) Try Try but never cry print(f.read(4)) print(f.read(5)) print(f.read()) 18. Write a program in python to read first line from the file(“data.txt”) 19. What is the output of following code? (i) file(“e:\\poem.txt”,”r”).readline() (ii) file(“e:\\poem.txt”,”r”).readline() If the file “poem.txt” contains following text: Nature moves in a spiral as do our personal lives. One who in tune with nature is in tune with the practice of living. What is the output of the following code? fh=open(“poem.txt”,”r”) size=len(fh.read()) print(fh.read(5)) 20. The file “Tree.txt” contains the following: I think that I shall never see A poem lovely as a tree. A tree that looks at God all day. And lifts her leafy arms to pray. What will be the output produced by the following code? obj1=open(“Tree.txt”, “r”) s1=obj1.readline() s2=obj1.readline() s3=obj1.read(15) print(s4) obj1.close() 21. Accept five sports names from the user and write in a file “sport.txt” (each name should write in separate line) 22. Suppose content of 'Myfile.txt' is Honesty is the best policy. What will be the output of the following code? myfile = open("Myfile.txt") x = myfile.read() print(len(x)) myfile.close() 23. Explain the syntax of seek() function with an example.What happens if no arguments are passed to the seek function? 24. What is the use of flush() function? 25. Explain the difference between Absolute and Relative file path? 26. Suppose content of 'Myfile.txt' is Ek Bharat Shreshtha Bharat What will be the output of the following code? myfile = open("Myfile.txt") vlist = list("aeiouAEIOU") vc=0 x = myfile.read() for y in x: if(y in vlist): vc+=1 print(vc) myfile.close() 27. Suppose the content of a text file Notes.txt is: “The way to get started is to quit talking and begin doing” What will bet the output of the following Python code? F= open(Notes.txt) F.seek(29) S=F.read() print(S)
28. Suppose the content of “Rhymes.txt” is
Good Morning Madam What will be the output of the following Python code? F=Open(“Rhymes.txt”) L=F.read().split() for W in L: if W.lower()==w[: :-1].lower(): print(W) 29. Write a Python code to read the file “Story1.txt” and copy only those lines in another file “Story2.txt” which are not starting with alphabet ‘T’. 30. Write a python program that appends the content of one file “Notes.txt” into another file “Story.txt”.