Unit 6 File Handling 5th sem New Syllabus June 6, 2024June 26, 2024BIM NotesLeave a Comment on Unit 6 File Handling Unit 6 File Handling 1 / 14 How do you check if a file exists before trying to open it ? a) Use 'file.exists("filename")' b) Use 'os.file_exists("filename")' c) Use 'os.path.exists("filename")' d) Use 'os.checkfile("filename")' 2 / 14 Which of the following is the default mode for opening a file in Python? a) 'r' b) 'w' c) 'a' d) 'r+' 3 / 14 What does the mode 'w' do when opening a file? a) Opens the file for reading b) Opens the file for writing, and truncates the file to zero length if it exists c) Opens the file for appending d) Opens the file for both reading and writing 4 / 14 If a file is opened in 'a' mode, what happens if the file does not exist? a) An error is raised b) The file is opened in read-only mode c) The file is created d) The file is truncated to zero lengt 5 / 14 What does the 'b' character signify when opening a file? a) Open the file in binary mode b) Open the file in buffered mode c) Open the file in both read and write mode d) Open the file without buffering 6 / 14 Which method is used to read the entire content of a file as a string? a) read() b) readline() c) readlines() d) file.read() 7 / 14 Which method is used to read a single line from a file? a) read() b) readline() c) readlines() d) file.readline() 8 / 14 To write a list of strings to a file, which method would you use? a) write() b) writelines() c) writefile() d) file.write() 9 / 14 Which method can be used to ensure all changes are saved to a file? a) save() b) flush() c) sync() d) close() 10 / 14 Which of the following functions is used to get the current working directory? a) os.getcwd() b) os.getcwdir() c) os.curdir() d) os.listdir() 11 / 14 How do you rename a file using the os module? a) os.renamefile() b) os.move() c) os.rename() d) os.rename_to() 12 / 14 Which method from the os module is used to create a directory? a) os.mkdir() b) os.makedir() c) os.createdir() d) os.makedirs() 13 / 14 Which of the following is a correct usage of the with statement to open a file for reading? a) with open("file.txt", "w") as f: b) with open("file.txt", "a") as f: c) with open("file.txt", "r") as f: d) with open("file.txt", "rb") as f: 14 / 14 how do you open a file for both reading and writing (without truncating the file)? a) 'r+' b) 'w+' c) 'a+' d) 'rw Your score isThe average score is 0% 0% Restart quiz