Python File Handling

Last Updated :
Discuss
Comments

Question 1

What is file handling in Python?

  • A

    Managing folders

  • B

    Reading and writing file

  • C

    Controlling system files

  • D

    Handling exceptions in files

Question 2

Which function is used to open a file in Python?

  • A

    open_file()

  • B

    read_file()

  • C

    file_open()

  • D

    open()

Question 3

Which method is used to read the entire content of a file as a string?

  • A

    read()

  • B

    read_all()

  • C

    read_full()

  • D

    readfile()

Question 4

What is the purpose of the 'rb' mode in file opening?

  • A

     Read binary

  • B

     Replace binary

  • C

    Read and backup

  • D

    Rename binary

Question 5

What is the purpose of the 'a' mode in file opening?

  • A

    Append

  • B

    Add

  • C

    Access

  • D

    Assign

Question 6

How can you read the first five characters of a file in Python?

  • A

    file.read(5)

  • B

    read(file, 5)

  • C

    read_first(file, 5)

  • D

    file.read_first(5)

Question 7

How do you check whether a file or directory exists in Python?

  • A

    check_file_existence()

  • B

    file_exists()

  • C

    os.path.exists()

  • D

    verify_file()

Question 8

What is the purpose of the 'with' statement in file handling?

  • A

    It creates a new file

  • B

    It automatically closes the file

  • C

    It appends data to a file

  • D

    It reads the entire file at once

Question 9

How do you read a file line by line in Python efficiently?

  • A

    read_all_lines()

  • B

    file.read_line()

  • C

    Iterate over the file object (e.g., for line in file:)

  • D

    read_line(file)

Question 10

How can you write data to a file without overwriting its existing content?

  • A

    Open the file with mode 'w' and call write()

  • B

    Open the file with mode 'a' and call write()

  • C

    Use file.write_append()

  • D

    Use file.write('a')

There are 20 questions to complete.

Take a part in the ongoing discussion