Ans To Worksheet Theory
Ans To Worksheet Theory
Ans To Worksheet Theory
Ans: "a" mode (append mode): Adds new content to the end of the file without deleting
what's already there. If the file doesn't exist, it creates a new one.
"w" mode (write mode): Deletes all existing content in the file before writing new content.
If the file doesn't exist, it creates a new one.
"r" (read mode): Opens the file for reading. The file must exist, and its content is
preserved. No writing or modifying is allowed.
Ans: readline(): Reads one line from the file each time you call it.
readlines(): Reads all the lines from the file at once and returns them as a list.
Ans: The advantage of opening a file using the "with" keyword is that it automatically
manages resources, ensuring that the file is properly closed after its block of code is
executed.
writelines(): Writes a list of strings to the file, each string as a separate line
13)What is a File?
Ans: a file is a collection of data stored in a computer that is given a name and a location
on the storage device. It can contain text, images, programs, or any other type of
information.
14)What is-
a)Text File- A text file in Python is a file that stores data in the form of plain text, containing
human-readable characters like letters, numbers, and symbols, rather than binary data.
b)Binary File- A binary file in Python is a type of file that contains data in a format that is not
meant to be human-readable, typically composed of 0s and 1s, representing raw data rather than
text.
c)CSV File- A CSV file in Python is a plain text file where data is separated by commas, with each
line representing a row of data and each comma separating different values.
15)Difference between read() and read(n):
Ans: Simple: Creating, reading, and editing CSV files is easy with basic text editors or
spreadsheet software.
Compatible: CSV files work with many software applications, making them versatile for
sharing data.
Compact: They have small file sizes, saving storage space and speeding up data transfer.
Readable: CSV files are plain text, making their content easily readable by both humans and
machines, aiding in transparency and problem-solving.
Ans: open:
with open:
19)Types of Errors:
KeyError: Happens when trying to access a key that does not exist in a dictionary.
AttributeError: Occurs when trying to access or use an attribute of an object that does
not exist.
ImportError: Raised when an import statement fails to find the module.
RuntimeError: General error indicating a problem occurred that does not belong to any
specific category.
EOFError: An EOFError occurs when Python tries to read input or a file but
unexpectedly reaches the end of the file.