Computer Science Class XII Python File Handling Test Series Part 3

Computer Science Class XII Python File Handling Test Series Part 3

Q1. What is the difference between r+ and w+ mode?

Q2. What is the difference between write and append mode?

Q3. Which method is used to close the file in python?

Q4. Write the statement to close the file "test.txt" which is associated with file object named "fo".

Q5. Write one similarity and one difference between a+ and w+.

Q6. What is the difference between read() and read(n) functions?

Q7. What is the difference between readline() and readlines() ?

Q8. Write a program to read first 10 characters from a file named "data.txt"

Q9. Write a program to read entire content from the file named "test.txt"

Q10. 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())


SOLUTIONS

Q1. What is the difference between r+ and w+ mode?

Ans. r+ mode will return error if file does not exist while w+ mode will create a new file if file does not exist.

Q2. What is the difference between write and append mode?

Ans. Write mode over writes the existing data while append mode add the new data at the end of the file.

Q3. Which method is used to close the file in python?

Ans. close()

Q4. Write the statement to close the file "test.txt" which is associated with file object named "fo".

Ans. fo.close()

Q5. Write one similarity and one difference between a+ and w+.

Ans. Similarity : In both the modes, we can do read and write operations.

Difference : In w+ mode file will be truncated (previous data lost) while in a+ mode, file's existing data will not be deleted and new data will be added at the end of the file.

Q6. What is the difference between read() and read(n) functions?

Ans. read() : will read entire data from the file.

        read(n) : will read first n characters/bytes from the file.

Q7. What is the difference between readline() and readlines() ?

Ans. readline() :This function will read one line from the file.

        readlines() : This function will read all the lines from the files.

Q8. Write a program to read first 10 characters from a file named "data.txt"

Ans. 

f = open("data.txt","r")

data = f.read(10)

print(data)

Q9. Write a program to read entire content from the file named "test.txt"

Ans. 

f = open("test.txt, "r")

d = f.read()

print(d)

Q10. 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())

Ans.

Try

Try b

ut never cry

No comments:

Post a Comment

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet