Q: Reading a file's first 30 bytes and printing it
My question is how do you count the number of bytes given.
below is the poem.
please help
Answers
Answer:
Give one difference between Text file and Binary File
Ans Text file contains EOL character at the end of every line, there is no such character
in binary file
2 Write a Python statement to open a text file “DATA.TXT” so that new contents can
be written on it.
Ans f = open(„DATA.TXT‟,‟w‟)
3 Write a Python statement to open a text file “DATA.TXT” so that new content can be
added to the end of file
Ans f = open(„DATA.TXT‟,‟a‟)
4 Write a Python statement to open a text file “DATA.TXT” so that existing contents
can be read from file.
Ans f = open(„DATA.TXT‟)
5 A file “MYDATA.TXT” is opened as
file1 = open(“MYDATA.TXT”)
Write a Python statement to close this file.
Ans file1.close()
6 What is the different in file opening mode “a” and “w” ?
Ans “w” is used to write in file from the beginning. If file already exists then it will
overwrite the previous content.
“a” (append – add at the end ) is also used to write in file. If file already exists it will
write after the previous content i.e. it will not overwrite the previous content and
add new content after the existing content.
6 What is the significance of adding „+‟ with file opening mode, with context to „r+‟ ?
Ans “+” is used to add alternate action with specified mode i.e. if used with “r” as “r+” it
means it will allows to read and alternate action write.
7 What is the difference between readline() and readlines() ?
Ans readline() allows to read single line from file and return the content as string.
readlines() function will read all the lines from file and return it as a List of
lines/string.
8 What is the purpose of using flush() in file handling operations ?
Ans When we are writing data in file the content will be stored in file only when we close
the file. Before closing the file i.e. during the operations fill will be created but the
content will be in buffer not in file and when we close the file content will be shifted
to file from buffer.
flush() allows the user to send content in file before closing the file. It means when
flush() is used it will clear the buffer and transfer content to file.
9 What is the advantage of opening file using „with‟ keyword?
Ans With keyword reduces the overheads involve in file handling operations like closing
the file after operation or handling the file closing with exceptions. When file is
opened using “with” it will manage these things i.e. file will be automatically closed
after operations. It ensures the closing of file even if exceptions arises.